<?php
// 1. create a database connection
$dbhost = "localhost" ;
$dbuser = "widget_cms";
$dbpass = "secretpassword";
$dbname = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// test if connection occured
if(mysqli_connect_errno())
{
die("database connection failed:". mysqli_connect_error() .
" " . mysqli_connect_errno() . " " );
}
?>
<?php
//often these are $_POST values through a web form.
// 2. Perform the database query
$id = 5;
$menu_name = "delete me";
$position = 4;
$visible = 1;
$query = "UPDATE subjects SET ";
$query .= "menu_name = '{ menu_name}', ";
$query .= "position = {$position}, ";
$query .= "visible = {$visible}, ";
$query .= "WHERE id = {$id} ";
$result = mysqli_query($connection, $query);
if ($result)
{
//success
//redirect to somepage.php
echo "success!";
}
else
{
//failure
//$message = "subjet update failed.";
die("database query failed. " . mysqli_error($connection));
}
?>
<DOCTYPE HTML>
<head>
<title> databases </title>
</head>
<body>
</body>
</html>
<?php
// 5. close the database connection
mysqli_close($connection);
?>
当我尝试在firefox中运行此页面时,这是弹出的错误。 而且我不知道如何解决它。请帮忙!
&#34;数据库查询失败。您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在#&lt; WHERE id = 5&#39;附近使用正确的语法。在第1行&#34;
TYSM。 :)
答案 0 :(得分:0)
1
if (mysqli_connect_error()) {
die('Database connection failed: ' . mysqli_connect_error()); // remove second mysqli_connect_error()
}
2
$query .= "menu_name = '{ $menu_name}', "; // $ missed
3
$query .= "visible = {$visible} "; // remove trailing `,`
注意: - 这不是结束,可以在代码中完成很多改进。