<?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中運行此頁面時,這是彈出的錯誤。 而我不知道如何解決它。請幫忙!mariadb(xampp)出現意外的語法錯誤
「數據庫查詢失敗,您必須在您的SQL語法錯誤;檢查對應於您MariaDB的服務器版本使用附近的正確語法手冊‘WHERE ID = 5’位於第1行」
TYSM 。 :)
你在'menu_name'之前缺少'$'。 – Barmar
'$ query。=「visible = {$ visible},」;'注意結尾',' –
它現在正在工作。還有一件事,當我刷新頁面一次時,「成功」就會得到迴應。當我再次刷新頁面(第二次)時,它顯示「databasequery failed」,沒有任何實際的錯誤信息。是因爲沒有更新? – scarecrow