0

我有以下代码更新数据库表中的字段以向其添加 1。ID 应该有一个发布到它的变量。谁能看到这是哪里出错了?

编辑:对不起,我完全忘记提及实际问题。数据库未更新该字段。

<?php
$con=mysqli_connect("localhost","**","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"UPDATE base SET thumbdown = thumbdown + 1 WHERE id = $_POST['id']");

mysqli_close($con);
?>
4

1 回答 1

3

因为它是一个数组,所以您不会$_POST['id']正确地将解析到字符串中。您必须将其括在大括号中:

"UPDATE base SET thumbdown = thumbdown + 1 WHERE id = '{$_POST['id']}'"

但请记住(总是!)首先转义变量:

$id = mysqli_real_escape_string($_POST['id']);
mysqli_query($con,"UPDATE base SET thumbdown = thumbdown + 1 WHERE id = '{$id}'");
于 2013-11-07T18:32:42.630 回答