插入新事件时SQL语法出错

时间:2017-04-27 02:05:15

标签: php mysql syntax syntax-error

尝试在我的页面上添加新事件时出现此错误。有人可以帮忙吗?我已经尝试了一切,并且是初学者。提前谢谢!

'SQL语法错误:查询失败:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在“第2行”附近使用正确的语法

<?php
if(session_status() != PHP_SESSION_ACTIVE) {
session_start();
}
if (isset($_SESSION["username"])) { 
require_once("db.php");
$results = ""; 
$venue = "";  
$eventname = ""; 
$startdate = ""; 
$starttime ="";  
$enddate = ""; 
$endtime = ""; 


$query = "SELECT DISTINCT venue
      FROM event
      ORDER BY venue";

$results = mysqli_query($con, $query) or die("Query failed: " .     mysqli_error($con));

while ($row = mysqli_fetch_array($results)) {
$venue .= "<option>{$row['venue']}</option>";
}

$eventname .= "<option>{$row["event_name"]} </option>";
 $startdate .= "<option>{$row["begin_date"]}
</option>";


$enddate .= "<option>{$row["end_date"]}
</option>";


mysqli_free_result($results);
mysqli_close($con);}
?>

<!doctype html>
<html>
<head>

<title>Adding a New Event</title>
<link href="ODC.css" rel="stylesheet">

</head>
<body>
<img class="homepageback" width="150" height="150" src= "https://s-media-cache-ak0.pinimg.com/originals/85/86/3b/85863b5dcd05835a7d12db5d88115b70.jpg"/>


<div class="container">

  <center><img class="displayed" width="150" height="150" src= "https://pbs.twimg.com/profile_images/816431933081014272/kXQVW1xX.jpg"/></center>
  <h3>Add a New Event</h3>
  <form method="post" action="enter-event-insert.php">

    <div><label for="event_name">Event Name: </label><input name="event_name"></div>

    <div><label for="venue []">Event Venue: </label><select multiple name="venue">
      <option value="a">A</option>
      <option value="b">B</option>
      <option value="c">C</option>
      <option value="d">D</option>
<?php
foreach ($_POST['venue'] as $names)
{
print "Your Venue is Selected $names<br/>";
}

?>
</select>
</div>

<div><label for="begin_date">Start Date And Time: </label><input     type="datetime-local" name="begin_date"></div>

<div><label for="end_date">End Date And Time: </label><input type="datetime-local" name="end_date"></div>

    <div><input class="button" type="submit" name="submit"></div>

  </form>


</div>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

在我无法将值插入数据库之前,我遇到过这样的问题,但是,在phpmyadmin中运行时的语句运行正常。首先,我强烈建议您先在phpmyadmin中执行语句,确保它已正确输入。接下来,如果语句执行正常但代码仍然无效,请尝试在字段中使用反引号(``)来查看是否有帮助(修复了我的问题)。为了将来参考,您还可以使用this MySQL syntax checker之类的网站来验证您的语法是否正确(我已经为您检查过,并且确实如此)。