| | |
Updating multiple rows in mysql...how to add a checkbox!
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi there,
I have successfully setup a page using a tutorial (shown http://www.phpeasystep.com/mysql/10.html)
It's all working fine as it stands.
I have now added another column to the database that can either be 1 or 0 and I would like to be able to change it on the update page using a checkbox for each record.
The code I tried was as follows:
What happens is weird - no matter which checkboxes are ticked, the database appears to update the wrong records. For example, if I had checked every other record (4 in total) and clicked submit, the database would have simply changed the top four records, not necessarily the ones I had originally selected.
Any ideas?!
Thanks,
Ben
I have successfully setup a page using a tutorial (shown http://www.phpeasystep.com/mysql/10.html)
It's all working fine as it stands.
I have now added another column to the database that can either be 1 or 0 and I would like to be able to change it on the update page using a checkbox for each record.
The code I tried was as follows:
PHP Syntax (Toggle Plain Text)
<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=""> <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"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td> <td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td> <td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td> <td align="center"><input name="ONOFF[]" 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){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]', ONOFF='$ONOFF[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close(); ?>
What happens is weird - no matter which checkboxes are ticked, the database appears to update the wrong records. For example, if I had checked every other record (4 in total) and clicked submit, the database would have simply changed the top four records, not necessarily the ones I had originally selected.
Any ideas?!
Thanks,
Ben
Last edited by Tekmaven; Jul 16th, 2008 at 3:10 pm. Reason: Code tags
Try this
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.
php Syntax (Toggle Plain Text)
<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.
Last edited by R0bb0b; Jul 16th, 2008 at 3:42 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
I'm getting a Warning: Cannot modify header information - headers already sent by from
when the script hits
Otherwise it works like a charm. Any idea as to how to avoid this error?
PHP Syntax (Toggle Plain Text)
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
when the script hits
PHP Syntax (Toggle Plain Text)
if($result1){ header("location:update_multiple.php"); }
Otherwise it works like a charm. Any idea as to how to avoid this error?
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
Using the code listed above, this is a subset of code I used for Radio buttons instead of checkboxes.
In addition, I was having trouble detecting the Submit button being used, so that section of the code was changed.
In addition, I was having trouble detecting the Submit button being used, so that section of the code was changed.
php Syntax (Toggle Plain Text)
if ($row['YesNo']=="Yes") { echo "<td><INPUT type=\"radio\" name=\"YesNo".$row['ID']."\" value=\"Yes\" CHECKED> Yes <INPUT type=\"radio\" name=\"YesNo".$row['ID']."\" value=\"No\" > No</td>"; } else { echo "<td><INPUT type=\"radio\" name=\"YesNo".$row['ID']."\" value=\"Yes\" > Yes <INPUT type=\"radio\" name=\"YesNo".$row['ID']."\" value=\"No\" CHECKED> No</td>"; } echo "</tr>"; } echo "</table>"; } ?> <input type="Submit" name="Submit" value="Submit"> </form> <?php Interviewed='".$_POST['YesNo'.$id]."' WHERE ID=".$_POST['id']; //echo "<p>SQL:".$sql1; //check if Submit button is active if (isset($_POST['Submit'])) { echo "<p>submit pressed"; foreach($_POST['id'] as $id) { $sql1="UPDATE table SET YesNo='".$_POST['YesNo'.$id]."' WHERE ID=".$id; $result1=mysql_query($sql1); echo "<p>".$sql1; } }
![]() |
Other Threads in the PHP Forum
- Previous Thread: AjAx problem in IE????
- Next Thread: mail failed from localhost
| Thread Tools | Search this Thread |
apache api array basic beginner binary body broken cakephp checkbox class cms code computing cron curl database date date/time delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert ip javascript joomla limit link list login mail memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms snippet source space sql static syntax system table thesishelp tutorial update upload url validator variable video web webdesign xml youtube





