我正在尝试点击图片并通过onclick Javascript函数将数据库值更改为“0”。我看了几个例子,但似乎无法使它发挥作用。
How can you use php in a javascript function
How to update data using onclick even CHECKBOX without button submit in php and mysql
Update Database from Javascript onClick By Ajax PHP
这是index.html文件
<script language="javascript">
function update() {
$.post("update.php", { num: 0 }, function(result) {
alert(result);
});
}
</script>
<img src="image.png" onclick="update()">
这是update.php文件。
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$num = $_POST["num"];
$id = 1;
$sql2 = "UPDATE db SET Column1='$num' WHERE id='$id'";
if ($conn->query($sql2) === TRUE) {
echo "Updated";
} else {
echo "Failed";
}
有人请指出我做错了吗?
答案 0 :(得分:0)
希望你的表名不是db
你应该使用表名(而不是db),如果column1和id的值是数字,你就不需要引号
$sql2 = "UPDATE your_table_name SET Column1=$num WHERE id=$id";