PHP插入MySQL数据库不起作用

时间:2018-04-21 22:35:04

标签: php mysql database sql-insert

我正在尝试将数据输入MySQL数据库。我可以登录数据库。但是,每当我运行时,错误“错误查询数据库2”一直出现。

我怀疑我的SQL查询有问题。但是,我已多次检查我的SQL查询,但我找不到任何错误。 (还没有)

感谢任何帮助!

<!DOCTYPE HTML>
<html>
<head>
<title>Create Events</title>
<link rel="stylesheet" href="RegisterLogin.css">
</head>


 <?php
    session_start();
    if (isset($_SESSION['Username'])) {
        $Username=$_SESSION['Username'];

    }
?>
<body>


<?php
    //define variables and set to empty values
     $EventNameErr = $MembersAttending_Err = $EventDateErr = $LocationErr = $websiteErr = "";
     $EventName = $MembersAttending = $EventDate = $Location = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
         if (empty($_POST["EventName"])) {
            $EventNameErr = "A name for the event is required";
          } else {
            $EventName = test_input($_POST["EventName"]);  
          }

          if (empty($_POST["MembersAttending"])) {
            $MembersAttendingErr = "How many members are attending";
          } else {
            $MembersAttending = test_input($_POST["MembersAttending"]); 
          }

           if (empty($_POST["EventDate"])) {
            $EventDateErr = "The date of the event is required";
          } else {
            $EventDate = test_input($_POST["EventDate"]); 
          }

          if (empty($_POST["Location"])) {
            $LocationErr = "Location of the event is required";
          } else {
            $Location = test_input($_POST["Location"]); 
          }



        //continues to target page if all validation is passed
        if ( $EventNameErr ==""&& $MembersAttendingErr ==""&& $EventDateErr ==""&& $LocationErr == ""){
            // check if exists in database
            $dbc=mysqli_connect('localhost','testuser','password','Project')
            or die("Could not Connect!\n");
            $sql="SELECT * from Events WHERE EventName ='$EventName';";
            $result =mysqli_Query($dbc,$sql) or die (" Error querying database 1");
            $a=mysqli_num_rows($result);
            if ($a>0){
            $EventNameErr="Event Name already exists".$a;
            } else {

            $sql1="INSERT INTO Events VALUES(NULL,'$EventName','$MembersAttending','$EventDate','$Location');";
                $result =mysqli_Query($dbc,$sql1) or die (" Error querying database 2");
                mysqli_close();
            header('Location: /EventCreated.php');
            }

        }
    }

       // clears spaces etc to prep data for testing
    function test_input($data){
        $data=trim ($data); // gets rid of extra spaces befor and after
        $data=stripslashes($data); //gets rid of any slashes
        $data=htmlspecialchars($data); //converts any symbols usch as < and > to special characters
        return $data;
    }

?>
<h2 style="color:yellow" align="center"> Event Creation </h2>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" align="center" style="color:#40ff00">
    EventName: 
    <input type="text" name="EventName" value="<?php echo $EventName;?>"/>
    <span class="error">* <?php echo $EventNameErr;?></span>
    <br/><br/>

    Members:
    <input type="text"  name="MembersAttending" value="<?php echo $MembersAttending;?>"/>
    <span class="error">* <?php echo $MembersAttendingErr;?></span>
    <br/><br/>

    Date:
    <input type="text"  name="EventDate" value="<?php echo $EventDate;?>"/>
    <span class="error">* <?php echo $EventDateErr;?></span>
    <br/><br/>

    Location:
    <input type="text" name="Location" value="<?php echo $Location;?>"/>
    <span class="error">* <?php echo $LocationErr;?></span>
    <br/><br/>



    <input type="Reset"  name="Reset" value="Reset">
    <input type="submit" name="submit" value="Submit"/> 
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:-1)

我不确定您的表中可用的列名是什么,但请尝试使用以下查询

我从你的代码中得到了列名,我不确定它是对还是错。就试一试吧。

$sql1="INSERT INTO Events (EventName,MembersAttending,EventDate,Location)
VALUES('$EventName','$MembersAttending','$EventDate','$Location');";