由于某种原因,我的SQL UPDATE语句无效。
$con=mysqli_connect("localhost","admin","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$items = array();
$desc = array();
$price = array();
$quantity = array();
$p=0;
while($inventorynumber>$p)
{
$invid[$p] = $_POST['invid'.$p.''];
$items[$p] = $_POST['item'.$p.''];
$desc[$p] = $_POST['desc'.$p.''];
$price[$p] = $_POST['price'.$p.''];
$quantity[$p] = $_POST['quantity'.$p.''];
$sql = "UPDATE `inventory_db` SET `item`='$items[$p]',`description`='$desc[$p]',`quantity`='$quantity[$p]',`price`='$price[$p]' WHERE `invid` = '$invid[$p]'";
$p++;
}
有更有效的方法吗?或者我需要添加一个mysqli_close($ con);后?
答案 0 :(得分:0)
我会在循环中运行sql查询。除非您稍后在某处使用阵列,否则可以简化此操作。目前尚不清楚$ inventorynumber的设置位置。另外,请记住设置自动提交。
mysqli_autocommit($con, TRUE);
$p=0;
while($inventorynumber > $p) {
$invid = $_POST['invid'.$p.''];
$item = $_POST['item'.$p.''];
$desc = $_POST['desc'.$p.''];
$price = $_POST['price'.$p.''];
$quantity = $_POST['quantity'.$p.''];
$sql = "UPDATE `inventory_db` SET `item`='$items[$p]',`description`='$desc[$p]',`quantity`='$quantity[$p]',`price`='$price[$p]' WHERE `invid` = '$invid[$p]'";
mysqli_query($conn, $sql);
$p++;
}
mysqli_close($conn);