-1

For some reason my SQL UPDATE statement is not working.

    $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++;
    }

Is there a more efficient way of doing this? Or do I need to add in a mysqli_close($con); after?

1 Answers1

0

I would run the sql query in the loop itself. Unless you're using the array somewhere later, you can simplify this. It's not clear where $inventorynumber is being set. Also, remember to set autocommit.

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);
  • Inventory number is being set in $_SESSION['inventorynumber'] but on second thought I might change it to $_POST – user3607800 May 10 '14 at 05:59