Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating more then one post at a time? 2

Status
Not open for further replies.
Joined
Jun 9, 2004
Messages
188
Location
US
Hello I trying to have a page update all users ages at once using php/mysql. Justt for example purposes I chose this 'age senario'.

I had it working by updating one person at a time, but when I tried to display all users to the page and update everyones age at once via a post...that is when I got into trouble.

My question is..do I need a loop when I execute: $query = mysql_query ("UPDATE user set age = ' $age' WHERE id = $id");

Any help would be very helpful. thanks much.

A small example of my code:
Code:
if($do =="modify")
        {
        <form action=update.php method=post>
	<input type=hidden name=do value=modify_age>

	$query="select * from users order by last ASC";
	$result=mysql_query($query);
	while($rs1=mysql_fetch_array($result)) 
	     {
		
               echo "
	       
               
	       "<input type=hidden name=id value=$rs1[last_name]>

	       Update user $rs1[last_name] Age:
               <input type=text name=age  size=3 value=\"
                \"><br>\n";
	
              }
        echo <input type=submit value=\"Modify\"></form>";
	

        }

if($do == "modify_age")
     {
	
	$query = mysql_query ("UPDATE users set age = ' $age' WHERE id = $id");
	
	echo "Modified successfully"	
	";

    }
 
Would the loop go in the second part of the script?


Thank you.
 
Th trick to is associate each record with an id to update the correct record. Therefore a solution needs to be found where the records are matched. One way is an array. Each
id is in the same place in the array as the age to be edited. Arrays are indicated with the [] after the element name, like this:
Code:
echo "<input type='hidden' name='id[]' value='".$rs1[last_name]."'>Update user ".$rs1[last_name]." Age:
               <input type=text name=age[]  size=3 value=\"
                \"><br>\n";

When iterating thru the array on the server each id will then have the same ordinal (or place in the array) as the age corresponding to that user. (Note: name is not necessarily the best id, since you may have two "Smith" or "Jones" ( a unique numerical id (autonumber or other primary key would be better)

Another option I have used is to concatenate the id into the name of the id (in this case user) into the name of the age input box. This can then be split at the server to get the name and the value.

In any case, in answer to your question, the loop needs to be written in the second part of the script

Code:
if($do =="modify")
        {
        echo "<form action=update.php method=post>
    <input type=hidden name=do value=modify_age>";

    $query="select * from users order by last ASC";
    $result=mysql_query($query)or die ("Can't complete query");
    while($rs1=mysql_fetch_array($result)) 
         {
        
               echo "<input type=hidden name=id[] value=".$rs1[last_name].">

           Update user ".$rs1[last_name]." Age:
               <input type=text name=age[]  size=3 value=\"
                \"><br>\n";
    
              }
        echo <input type=submit value=\"Modify\"></form>";
    

}elseif($do == "modify_age"){
    //get the arrays
    $ids  = @$_POST['id'];
    $ages = @$_POST{'age'];

  //loop thru the array to update the values
  for($x = 0; $x < count($ids); $x++){

  //connection string info here

  //run query 
    $query = mysql_query ("UPDATE users set age = ".$ages[$x]". WHERE id = '".$ids[$x]."'");
    
    //check results 
    if ($query){
     echo "Modified successfully for user: ".$ids[$x]."<br>";    
     }//end if
   }//end for

}//end if

Bastien

Cat, the other other white meat
 
Th trick to is associate each record with an id to update the correct record. Therefore a solution needs to be found where the records are matched. One way is an array. Each
id is in the same place in the array as the age to be edited. Arrays are indicated with the [] after the element name, like this:
Code:
echo "<input type='hidden' name='id[]' value='".$rs1[last_name]."'>Update user ".$rs1[last_name]." Age:
               <input type=text name=age[]  size=3 value=\"
                \"><br>\n";

When iterating thru the array on the server each id will then have the same ordinal (or place in the array) as the age corresponding to that user. (Note: name is not necessarily the best id, since you may have two "Smith" or "Jones" ( a unique numerical id (autonumber or other primary key would be better)

Another option I have used is to concatenate the id into the name of the id (in this case user) into the name of the age input box. This can then be split at the server to get the name and the value.

In any case, in answer to your question, the loop needs to be written in the second part of the script

Code:
if($do =="modify")
        {
        echo "<form action=update.php method=post>
    <input type=hidden name=do value=modify_age>";

    $query="select * from users order by last ASC";
    $result=mysql_query($query)or die ("Can't complete query");
    while($rs1=mysql_fetch_array($result)) 
         {
        
               echo "<input type=hidden name=id[] value=".$rs1[last_name].">

           Update user ".$rs1[last_name]." Age:
               <input type=text name=age[]  size=3 value=\"
                \"><br>\n";
    
              }
        echo <input type=submit value=\"Modify\"></form>";
    

}elseif($do == "modify_age"){
    //get the arrays
    $ids  = @$_POST['id'];
    $ages = @$_POST{'age'];

  //loop thru the array to update the values
  for($x = 0; $x < count($ids); $x++){

  //connection string info here

  //run query 
    $query = mysql_query ("UPDATE users set age = ".$ages[$x]". WHERE id = '".$ids[$x]."'");
    
    //check results 
    if ($query){
     echo "Modified successfully for user: ".$ids[$x]."<br>";    
     }//end if
   }//end for

}//end if

Bastien

Cat, the other other white meat
 
That is some nice work. I understand it now. Thanks for your help. i will try to incorporate this example into what I am really trying to do.

Thank you both.
 
As Bastien has suggested, the code to loop through the input would be handled by whatever code you're using to handle the user's input.


The only change to Bastien's code I would make would be to drop the use of the "elseif" construct. I have no idea why "elseif" was included in the language -- there is nothing it does that cannot be done better by a nested "if" statement or a switch statement.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top