PHP查询失败,SQL查询工作?

时间:2013-05-23 00:16:34

标签: php mysql

我有一个重要的时刻,我现在一直在玩AGES的这段代码。我无法理解这一点。希望你们其中一个人可以帮我一臂之力。

我尽可能地评论了代码!

if(!empty($_POST['eventname'])) //Event Name isn't empty, you'll see why later!
{
    $sql = mysqli_query($link, "SELECT EventID FROM events"); //Grab all EventID's from events
    if($sql) //If ^THAT worked
    {
        $startd = $_POST['date'] . $_POST['date2'] . $_POST['date3']; //Joins 3 drop downs to form YYYYMMDD.
        $endd = $_POST['date4'] . $_POST['date5'] . $_POST['date6']; //Same as above.
        $found = false; //Does the EventID already exist?

        while($row = mysqli_fetch_row($sql))
        {
            if($row[0] == $_POST['eventid']) {$found = true;} //If so, found is true.
        }

        if($found==true)
        {
//Execute this, if found is true, found isn't true as I've no events yet, 
//so this doesn't matter too much.
            $sql2 = mysqli_query($link, 
              "UPDATE events SET EventName='" . 
              $_POST['eventname'] . 
              "', EventStart='" . $startd . 
              "', EventEnd='" . $endd . 
              "', EventDesc='" . $_POST['desc'] . 
              "' WHERE EventID='" . $_POST['eventid'] . 
              "', EventTime='" . $time . "'"); 


         if($sql2)
         {
             $success = "Update successful!"; //Allows me to add a message to user.
         } else {
              $failure = "$sql2 Error encountered: " . mysqli_error($link); //Same as above
         }
     } else {
         $sql2 = mysqli_query($link, "INSERT INTO events (EventName, EventStart, EventEnd, EventDesc, EventTime) VALUES ('" . $_POST['eventname'] . "', '" . $startd . "', '" . $endd . "', '" . $_POST['desc'] . "', '" . $_POST['stime'] . "'"); //This is the query that fails, the sql query goes: INSERT INTO events (EventName, EventStart, EventEnd, EventDesc, EventTime) VALUES ('First Audition', '20133005', '20133005', 'Auditions', '1200')

                        if($sql2)
                            {
                                $success = "Update successful!"; //Not seen this message yet :(
                            }
                        else
                            {
                                $failure = "sql2 Not Found. Error encountered: " . mysqli_error($link) . " / " . $_POST['eventname'] . " / " . $startd . " / " . $endd . " / " . $_POST['desc'] . " / " . $_POST['stime']; //Here is where all the variables are shown during the error message. I can see each and every single one and have more than triple checked this. The error message: sql2 Not Found. Error encountered: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
                            }
                    }
            }
    }

基本上,我无法弄清楚它为什么会失败,你们能帮我一把吗?希望我没有傻,因为已经很晚了。

非常感谢! 麦克

1 个答案:

答案 0 :(得分:3)

你没有在查询失败时正确关闭()

$sql2 = mysqli_query($link, "INSERT INTO events (EventName, EventStart, EventEnd, EventDesc, EventTime) VALUES ('" . $_POST['eventname'] . "', '" . $startd . "', '" . $endd . "', '" . $_POST['desc'] . "', '" . $_POST['stime'] . "')");