MySQL错误1064:您的SQL语法有错误..在第1行附近?

时间:2017-04-17 21:27:18

标签: php mysql-error-1064

我正在研究MySQL& PHP项目。它基于音乐数据库。当我转到http://andrewb1.sgedu.site/editgenres.php时,我收到以下错误:

Error: SQL Error: 
Errno: 1064
Error: 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行收到错误,因为唯一有开放的php标记

editgenres.php的代码是:

<?php

include 'dbconnect.php';

$sql = "select * from Genres where GenreID = " . $_REQUEST['GenreID'];
if (!$result = $mysqli->query($sql)) {
    echo "Error: SQL Error: </br>";
    echo "Errno: " . $mysqli->errno . "</br>";
    echo "Error: " . $mysqli->error . "</br>";

    exit;
}

$row = $result->fetch_assoc();

?>

<form action="editgenressrv.php">
<input type="hidden" name="GenreID" value = "<?php echo $row["GenreID"]?>"/> 
GenreID:<input type="text" name="GenreID" value="<?php echo $row["GenreID"]?>"/></br>
GenreName:<input type="text" name="GenreName" value="<?php echo $row["GenreName"]?>"/></br>
<input type="submit"/>
</form>

此外,如果需要,这里是EditGenresSrv.php的代码:     

include 'dbconnect.php';

$sql = "update Genres set ";
$sql .= "GenreID = '" . $_REQUEST["firstname"] ."'," ;
$sql .= "GenreName = '" . $_REQUEST["lastname"] ."'," ;
$sql .= "where GenreID= " . $_REQUEST['GenreID']; 
if (!$result = $mysqli->query($sql)) {
    echo "Error: SQL Error: </br>";
    echo "Errno: " . $mysqli->errno . "</br>";
    echo "Error: " . $mysqli->error . "</br>";

    exit;
}
?>
<script>
window.location='genres.php';
</script>

如果需要,这里是dbconnect.php(虽然我已经测试了它并且很好):     

include 'dbconnect.php';

$sql = "insert into students (firstname,lastname,email) values (" . 
  "'" . $_REQUEST["GenreID"] ."','" .
  $_REQUEST["GenreName"] . "' ";

if (!$result = $mysqli->query($sql)) {
    echo "Error: SQL Error: </br>";
    echo "Errno: " . $mysqli->errno . "</br>";
    echo "Error: " . $mysqli->error . "</br>";

    exit;
}
?>
<script>
window.location='genres.php';
</script>

这是HTM文件:

<form action="addgenressrv.php">
GenreID:<input type="text" name="GenreID"/></br>
GenreName:<input type="text" name="GenreName"/></br>

<input type="submit"/>
</form>

2 个答案:

答案 0 :(得分:1)

在逗号之前小心使用逗号。

$sql = "update Genres set ";
$sql .= "GenreID = '" . $_REQUEST["firstname"] ."'," ;
$sql .= "GenreName = '" . $_REQUEST["lastname"] ."' " ;
$sql .= "where GenreID= " . $_REQUEST['GenreID'];

答案 1 :(得分:1)

您需要将GenreID传递到您的页面,请查看以下链接

http://andrewb1.sgedu.site/editgenres.php?GenreID=1

你会明白一切。如果没有,那么我会解释你。 您的上一页应该有$_REQUEST['GenreID']的值。

$sql = "select * from Genres where GenreID = " . $_REQUEST['GenreID'];

这一行给出了错误消息,因为无论是使用POST方法还是GET,您都没有将GenreID传递给文件editgenres.php

在表单中添加<form action="editgenres.php">然后

GenreID:<input type="text" name="GenreID"/></br>
GenreName:<input type="text" name="GenreName"/></br>

因为当你用editgenres.php说错误时,你必须按照上面的表格调用此页面。检查您的第一页的操作,该页面将调用http://andrewb1.sgedu.site/editgenres.php