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參數。這是我唯一能想到的。 –