Hi
I tryed this code as it is and I have just modifyed the connexion parameters but it does'nt work correctly. each time I get the same resul and there is no modification in my database.
Could anyone help me please
Thnx a lot
Try this
<strong>Update multiple rows in mysql</strong><br> <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>On / Off</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)) { ?> <tr> <td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> <td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td> <td align="center"><input name="lastname<? echo $rows['id']; ?>" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td> <td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> ></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit) { foreach($_POST['id'] as $id) { $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', lastname='".$_POST["lastname".$id]."', email='".$_POST["email".$id]."', ONOFF='".$_POST["ONOFF".$id]."' WHERE id='".$id."'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close(); ?>
You were looping through with a counter and using the loopnumber as your id, this will only work if you are absolutely positive that your ids are sequential, looks like you are using register globals I think. I've never used them so I actually find your code confusing.