我的數據庫insert有一個表table1。表1有3列用戶ID,時間我想通過發佈從一個php文件中的值更新表1的時間列,但我的更新查詢,示值誤差:我已經插入ID列值php post方法在更新查詢中不起作用
Error: UPDATE table1 set time=1:40 WHERE id=12 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 ':40 WHERE id=12' at line 1
表1 ..what應該怎麼辦?這裏是我的代碼
update.php
<html>
<body>
<form action="newadd.php" method="post">
Name: <input type="text" name='name'><br>
<input type="submit" value="submit">
</form>
</body>
</html>
insertupdate.php
<?php
include('insertjoincon.php');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$tim = $_POST['name'];
$sql = "UPDATE table1 set time=$tim WHERE id=12";
if ($conn->query($sql) == TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
但是當我使用$tim = 4
然後表更新,但在使用後則顯示錯誤。
該字符串應該被引用''$ tim'',但那不是的話,你應該使用準備好的語句,因爲您的代碼易受sql注入攻擊。和你的通知消息是奇怪的,它說'創建新記錄',但你實際上更新行 – Ghost
'$ tim'必須在引號中...這些是SQL的基礎知識... –