Hi everyone,

I am trying to update multiple rows and have run into some difficulties. This code is from:

http://www.sitepoint.com/forums/show...te-MySQL-table


What I am doing is printing out the records of registered users. Each user has a certain number of computer stores, each store containing a number of computers with a few characteristics. So user1 might have 2 computer stores, the first store has 2 computers, the second store has one computer. In addition each store has information such as opening times and the store's location.

I'm using separate forms to update each store's information.

The updates work when I have one form input that loops multiple times. With only one form input the source code for the inputs will be:

<input type="hidden" name="id0" value="1001">
<input type="text" name="name0" value="Jet Black">
<input type="hidden" name="id1" value="1002">
<input type="text" name="name1" value="Dave Greenfield">


But with multiple form inputs in the loop I have inputs with the same name= number. For example, there could be 2 inputs that both have "id0" and "name0". This messes up the update query. This is probably because I don't know where to put $cnt +=1;


In addition:
Is it possible to only update the form inputs that have been filled in? If there are 7 inputs, the code will update all of them even if I only wish to update 1 input. This is most likely because of the $cnt variable. Also, is it possible to have only one update in the query instead of the update looping multiple times?



Please let me know if something isn't clear. Thank you in advance!



This is how the records for user1 might look like. Beneath each title will be a form input for the user to update the information

Jack's computer store
Opening times
location

Computer 1
speed
price
memory

Computer 2
speed
price
memory

Amy's computer store
Opening times
location

Computer 5
speed
price
memory


PHP Code:
<?php

$q 
"SELECT ... WHERE user_id={$_SESSION['user_id']} ";
$r = @mysqli_query ($dbc$q);     
$num mysqli_num_rows($r);

if (
$num 0) {
$cnt 0;



// loop #1
// loop out store names, opening times, location

while ($row mysqli_fetch_array($rMYSQLI_ASSOC)){
$fldname "name" $cnt;
$idname "id" $cnt;    

print 
'<form method="post" action="">';    
    
print 
'<p>'.$row['store_name'].'</p>
<p>store information</p>'
;

print
'<p>opening times: '.$row['opening_times'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row['store_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row['opening_times'];  
  print
'"  /> ';
  
print
'<p>location: '.$row['location'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row['store_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row['location'];  
  print
'"  /> ';
  
  
  
$storename $row['store_name'];


$q "SELECT  ... WHERE user_id={$_SESSION['user_id']} AND store_name  = '$storename'  ";
$r2 = @mysqli_query ($dbc$q);     
$num mysqli_num_rows($r2);
if (
$num 0) {
$cnt 0;

// loop #2
// looping out each store's computer characteristics

while ($row2 mysqli_fetch_array($r2MYSQLI_ASSOC)) {
$fldname "name" $cnt;
$idname "id" $cnt;    
    
    
print
'<p>'.$row2['computer_name'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row2['computer_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row2['computer_name'];  
  print
'"  /> ';
  
print
'<p>'.$row2['speed'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row2['computer_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row2['speed'];  
  print
'"  /> ';  
 
print
'<p>'.$row2['price'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row2['computer_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row2['price'];  
  print
'"  /> ';  
  
print
'<p>'.$row2['memory'].'</p>
  <input type="hidden" name="' 
$idname '" value="' $row2['computer_id'] .'"> 
  <input type="text" name="' 
$fldname '" size="" maxlength="" value="'
  echo 
$row2['memory'];  
  print
'"  /> ';   
  
  
  
  
$cnt +=1
}
print 
'<p><input type="hidden" name="count" value = "' $cnt '"><input type="submit"></p></form>';
}
}
}
 

if (
$_SERVER['REQUEST_METHOD'] == 'POST') {    

$process 0;  
$cnt $_POST['count'];
echo 
$cnt;
while (
$process $cnt) { 
   
$q "UPDATE computer SET computer_name='{$_POST['name' $process]}' WHERE computer_id = '{$_POST['id' $process]}'   ";
   
$r = @mysqli_query($dbc$q);   
   echo 
$q
   
$process += 1
   }
}






?>