我想更新一個已經創建的數據庫和表的數據,但它給錯誤:SQL更新查詢錯誤在PHP
錯誤更新記錄:您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用手動接近「其中EMPID = 1」在行1
這裏是代碼:
<!DOCTYPE html>
<?php
if(isset($_REQUEST['update']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bc140401259_DB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$employeeID="";
if (! empty($_POST['empID'])){
$employeeID = $_POST['empID'];
}
$Name="";
if (! empty($_POST['name'])){
$Name = $_POST['name'];
}
$Address="";
if (! empty($_POST['address'])){
$Address = $_POST['address'];
}
$Dateofbirth="";
if (! empty($_POST['DOB'])){
$Dateofbirth = $_POST['DOB'];
}
$Salary="";
if (! empty($_POST['salary'])){
$Salary = $_POST['salary'];
}
$timestamp="";
if (! empty($_POST['timeIn'])){
$timestamp = $_POST['timeIn'];
}
$sql = "UPDATE Employee SET Name = $Name, Address = $Address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestampemp where empID = $employeeID";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<html>
<body>
<form action="" method="post">
<h2>Add Employee Form</h2>
Employee ID: <input type="text" name="empID" >
<br><br>
Name: <input type="text" name="name" >
<br><br>
Address: <input type="text" name="address" >
<br><br>
Date of Birth: <input type="text" name="DOB" >
<br><br>
Salary: <input type="text" name="salary" >
<br><br>
Time In: <input type="text" name="timeIn" >
<br><br>
<input type="submit" name="update" value="UPDATE" />
</form>
</body>
</html>
php變量的名字是__case_sensitive__ –
第二 - 你查詢將失敗,因爲你不知道引號。 –
查看我的回答 –