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

时间:2018-06-06 07:23:42

标签: php mysql mysqli

嘿伙计我还在学习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());
   }
  ?>

1 个答案:

答案 0 :(得分:1)

因为 database.php

中存在语法错误

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

解决方案:

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

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