im trying to update multiple records in my table so im wondering if there´s something wrong in this script as im only getting updated the last record.. this is the complete code http://pastie.org/5453954
for ($i = 0; $i < count($_POST['id_servtype']); $i++){
$servtype = $_POST['id_servtype'][$i];
$project = $_POST['id_project'][$i];
$quantity = $_POST['tableQuantity'][$i];
$pus = $_POST['tablePus'][$i];
$puc = $_POST['tablePuc'][$i];
$totalitem = $_POST['tableTotal'][$i];
$sql = "update sales_order_items
set
id_project=?,
id_service_type=?,
quantity=?,
unit_price_no_tax=?,
unit_price=?,
total_item_imp=?
where
id_so=?
and
id_soitems=?
";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(1, $project, PDO::PARAM_INT);
$stmt->bindValue(2, $servtype, PDO::PARAM_INT);
$stmt->bindValue(3, $quantity, PDO::PARAM_STR);
$stmt->bindValue(4, $pus, PDO::PARAM_STR);
$stmt->bindValue(5, $puc, PDO::PARAM_STR);
$stmt->bindValue(6, $totalitem, PDO::PARAM_STR);
$stmt->bindValue(7, $_POST["id"], PDO::PARAM_STR);
$stmt->bindValue(8, $_POST["iditem"], PDO::PARAM_STR);
$stmt->execute();
}
echo("Correct edition");
There is something wrong with your logic: the values used in your
WHEREclause are always the same, so you’re always updating all records, each time using new values.Logically, when your script has finished, only the last update will be visible, all previous updates have been overwritten. A simple example:
Is the same as writing:
So the record(s) with id 1 are updated a total of three times, each time undoing the last update