//$update = $_POST;
//$quantity = $_POST;
//if($update){
//for($a=0;$a<$count;$a++){
mysql_query ("UPDATE productorder SET Product_quantity = '$quantity' WHERE id = '$id'");

THEY ONLY UPDATE THE LAST RECORD HOW I UPDATE THE MULITY RECORDS

Dani AI

Generated

Diagnosis and quick causes (references to the thread)
The behavior described by — three rows exist but only one ends up changed — is consistent with sending a single id/quantity pair to the server and repeating the same UPDATE. correctly flagged the page refresh inside the loop; an immediate redirect inside an iteration will abort further processing and can cause repeated submissions.

Practical fixes (form + server)

  • Submit per-row values as an array keyed by id (so the server receives something like quantity[123] => 2).
  • On the server treat the POSTed quantities as an associative array, validate/cast values, use a prepared statement and run updates inside a transaction, and perform the redirect only after all updates finish.

Example (safe, modern approach)

<?php
// assume $_POST['quantity'] is like ['3'=>2,'5'=>4]
$pdo = new PDO($dsn, $user, $pass, $opts);
$pdo->beginTransaction();
$stmt = $pdo->prepare('UPDATE productorder SET Product_quantity = :qty WHERE id = :id');
foreach ($_POST['quantity'] as $id => $qty) {
    $stmt->execute([':qty' => (int)$qty, ':id' => (int)$id]);
}
$pdo->commit();
header('Location: shoping_cart12.php');
exit;
?>

Bulk-update alternative
To reduce round-trips use a single query with CASE WHEN:

UPDATE productorder
SET Product_quantity = CASE id
  WHEN 3 THEN 2
  WHEN 5 THEN 4
  ELSE Product_quantity END
WHERE id IN (3,5);

Troubleshooting checklist and cautions

  • Dump POST data (print_r or var_export) to confirm array structure before updating.
  • Remove any redirect/refresh placed inside loops; follow PRG (POST→Redirect→GET).
  • Use prepared statements (PDO/MySQLi) instead of deprecated mysql_* calls, cast ids/quantities to integers, wrap multi-row changes in a transaction, and check affected rows for success.

Recommended Answers

All 10 Replies

Member Avatar for Member #120589

what's MULITY?

they only update the last row

they update only the last record

Member Avatar for Member #120589

Sorry I still don't understand what you mean by 'mulity' - I Googled it and couldn't find it.

//$update = $_POST['update'];
//$quantity = $_POST['quantity'];
//if($update){
//for($a=0;$a<$count;$a++){
mysql_query ("UPDATE productorder SET Product_quantity = '$quantity' WHERE id = '$id'");

This doesn't do anything does it? Your loop is commented out and your $id seems to be static. Odd.

i have 3 row(records) in database table like
id order quantity
1 2232 1
2 2232 1
3 2232 1
they pick the last row(records)
id order quantity
3 2232 1
and update
how i update the i and 2 rows(records)

Member Avatar for Member #120589

show your complete code -it's impossible to see what you're doing from the bit you included. You don't include a loop so I assume it is impossible to update all unique 'id' records otherwise.

<?php        
// ********** update ************ 
mysql_connect("$hostname_boss", "$username_boss", "$password_boss")or die("cannot connect");
mysql_select_db("$database_boss")or die("cannot select DB");
$sql="SELECT * FROM productorder";
$result=mysql_query($sql);
                            $count=mysql_num_rows($result);                 

$update = $_POST['update'];
$quantity = $_POST['quantity'];
$id = $_POST['id'];
if($update){
for($a=0;$a<$count;$a++){
$insert="UPDATE productorder SET Product_quantity = '$quantity' WHERE id = '$id'";  
$result1 = mysql_query($insert);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=shoping_cart12.php\">";

}}

?>
Member Avatar for Member #120589

first of all, you've included a meta refresh inside the loop. Don't do this. I'd use header() after the loop.

You're tables confuse me. I don't understand why you want to get the count of your orders and then loop over your insert statement that number of times.

UPDATE productorder SET Product_quantity = '$quantity' WHERE id = '$id'

This will only ever update one record as it is unique (I assume the id is a primary key).


I'm thinking that you've misunderstood what you're trying to do.

$update = $_POST;
$quantity = $_POST;
$id = $_POST;

$insert1="UPDATE productorder SET Product_quantity = '$quantity' where product_id = '$id'";
$result1= mysql_query($insert1);
// if successful redirect to delete_multiple.php
if($result1){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=shoping_cart.php\">";
}
mysql_close();

no dear they only pick the last record and update it
2 problem they continuously refresh the page
how i can i time refresh the page

Member Avatar for Member #120589

Sorry, I don't understand. Your code above should work. If you want to 'time refresh the page', do you mean pause for a while then refresh? If so:

<?php
header('Refresh: 5; URL=http://www.example.com');
?>
<p>You will be redirected in 5 seconds...</p>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.