1
我的数据库包含以下列:id, openingHours, records
。使用准备好的语句更新并使用ID绑定行
我有一个HTML表格,我从MySQL数据库中打印出行。在每个HTML行的右侧,我有一个按钮,我可以点击选择。当我点击选择时,我需要用+1对列records
进行更新,所属列id
。
查询是在phpMyAdmin工作,如果我使用这样的查询:
sqli = ("UPDATE stores SET records = records + 1 WHERE id = 333);
但我需要的ID绑定在哪里我使用select键行。当我用这个代码点击选择按钮时,phpMyAdmin中的行没有得到更新。我究竟做错了什么?我得到的错误:Undefined index: id in /Applications/MAMP/htdocs/storeproject/updaterecords.php on line 8 Updated Succesfull.
HTML
<form action="updaterecords.php" method="post">
<button type="submit" class="btn btn-success" name="submit">Select</button>
<?php include 'updaterecords.php' ?>
</form>
updaterecords.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); }
?>
<?php
if(isset($_POST['submit'])) {
$id = $_POST['id']; //line 8
$stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id =".$id);
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
$success = true;
}
$stmt->close();
$mysqli->close();
if($success) {
echo "Updated Succesfull";
} else {
echo "Failed: " . $stmt->error;
}
}
?>
ŧ要求您快速回答。我尝试了你的代码,至少现在我得到了一个错误:'注意:未定义的索引:id在第8行的/Applications/MAMP/htdocs/storeproject/updaterecords.php上 已更新Succesfull'。这是'$ id = $ _POST ['id'];''行。正如我看到的那样,当我在那里定义id时,id不应该是不确定的。 – Mimi
看来你没有从客户端获取id参数。这是我唯一能想到的。 –