我真的不明白为什么要把这个简单的任务搞得这么复杂。我正在尝试更新 mysql 数据库,但无论我做什么,它都不会更新!
我也不断收到此通知消息..
Notice: Undefined index: thisID in
基本上我得到了所有字段中正确回显的值,但是当我按下提交按钮时我无法更新 mysql,我得到了上面的错误!
我像这样从 listing.php 解析信息/数据
<?php
// This block grabs the whole list for viewing
$product_list = "";
$sql = "SELECT * FROM products ORDER BY date_added DESC";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$quantity = $row["quantity"];
$shipping = $row["shipping"];
$category = $row["category"];
$manufactor = $row["manufactor"];
$special = $row["special"];
$stock = $row["stock"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "Product ID: $id - <strong>$product_name</strong> - £$price - <em>Added $date_added</em> <a href='edit.php?pid=$id'>edit</a> • <a href='listing.php?deleteid=$id'>delete</a><br />";
}
} else {
$product_list = "You have no products listed in your store yet";
}
?>
这是编辑页面edit.php
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {
$pid = mysqli_real_escape_string($db_conx, $_POST['thisID']);
$product_name = mysqli_real_escape_string($db_conx, $_POST['product_name']);
$price = mysqli_real_escape_string($db_conx, $_POST['price']);
$quantity = mysqli_real_escape_string($db_conx, $_POST['quantity']);
$shipping = mysqli_real_escape_string($db_conx, $_POST['shipping']);
$category = mysqli_real_escape_string($db_conx, $_POST['category']);
$manufactor = mysqli_real_escape_string($db_conx, $_POST['manufactor']);
$special = mysqli_real_escape_string($db_conx, $_POST['special']);
$stock = mysqli_real_escape_string($db_conx, $_POST['stock']);
$details = mysqli_real_escape_string($db_conx, $_POST['details']);
// See if that product name is an identical match to another product in the system
$sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details', WHERE id=$pid";
if (!$sql) {
echo mysqli_errno($db_conx) . ": " . mysqli_error($db_conx) . "\n";
die();
}
$query = mysqli_query($db_conx, $sql);
header("location:");
exit();
}
?>
有人可以帮我解释一下吗?
提前致谢。
最佳答案
你的 `WHERE 子句之前有一个额外的逗号:
$sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details', WHERE id=$pid";
只需删除它,你应该没问题:
$sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details' WHERE id=$pid";
关于php - mysql UPDATE 不更新 mysql 数据库并返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364293/