gpt4 book ai didi

php - 尝试访问 mysql 数据库时出现 HTTP 错误

转载 作者:行者123 更新时间:2023-11-29 07:32:18 25 4
gpt4 key购买 nike

大家好,我还在学习 PHP,需要帮助。我有 3 个文件需要 database.php 文件才能连接到 MySQL。当我执行文件时,只有两个文件有效。对于最后一个文件,我收到 HTTP 错误 500。

    <?php

require( 'database.php');

// inserting information from the form into the database
$sql = ("INSERT INTO guestlist (firstName, lastName, phoneNumber,guests,event)
VALUES (?,?,?,?,?)");
// values are prepared to bind
$stmt = mysqli_prepare($con,$sql);

mysqli_stmt_bind_param($stmt,"sssss",$_POST['first_name'],$_POST['last_name'], $_POST['phonenumber'], $_POST['guest'], $_POST['event']);

$stmt->execute();




if (!$stmt)// Was not updated
{
// shows error


echo("There was an error with your RSVP. Please try again.");

mysqli_stmt_close($stmt);
}

else //Was updated
{
echo("Your RSVP has been completed.");
}

//End database connection
mysqli_close($con);

?>

这是我的教授提供的数据库文件

    <?php

$myHost = "localhost"; // localhost, do not change this string
$myUserName = "cmorales"; // CHANGE TO YOUR USERNAME
$myPassword = ""; // CHANGE TO YOUR PASSWORD
$myDataBaseName = "cmorales_project"; // CHANGE USERNAME
username_project

$con = mysqli_connect( "$myHost", "$myUserName", "$myPassword",
"$myDataBaseName" );

if( !$con ) // == null if creation of connection object failed
{
// report the error to the user, then exit program
die("connection object not created: ".mysqli_error($con));
}

if( mysqli_connect_errno() ) // returns false if no error occurred
{
// report the error to the user, then exit program
die("Connect failed: ".mysqli_connect_errno()." : ".
mysqli_connect_error());
}
?>

最佳答案

因为你的database.php

有语法错误

原因:username_project 第 7 行没有分号结尾。

解决方法:

删除第 7 行未定义的常量 username_project

正如@Antonio Teh Sumtin 在评论中提到的,在开发过程中始终检查错误日志或启用显示错误。

关于php - 尝试访问 mysql 数据库时出现 HTTP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50714497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com