| Updating multiple records
|
Jamier101

msg:4501357 | 9:22 pm on Sep 28, 2012 (gmt 0) | I've written a script that allows me to update a single record at a time using a text box and now I'd like to update multiple records using checkboxes. I think the update page might be missing some sort of loop but I'm unsure how to construct it and was hoping that someone could give me some pointers so that I can learn how to do it right. page1
<?php
// Get results from table $result = mysql_query("SELECT * FROM $tbl_name WHERE sent=0") or die(mysql_error());
echo "<table border='1'>"; echo "<tr> <th>ID</th> <th>First Name</th> <th>Surname</th> <th>Address1</th> <th>Address2</th> <th>Town/City</th> <th>County</th> <th>Post Code</th> <th>Request Date</th> <th>Sent</th> <th>-------</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )){ // Print out the contents of each row into a table echo "<tr><td>"; echo $row['request']; echo "</td><td>"; echo $row['firstname']; echo "</td><td>"; echo $row['surname']; echo "</td><td>"; echo $row['address1']; echo "</td><td>"; echo $row['address2']; echo "</td><td>"; echo $row['town']; echo "</td><td>"; echo $row['county']; echo "</td><td>"; echo $row['postcode']; echo "</td><td>"; echo $row['date']; echo "</td><td>"; echo "<input type='checkbox' name='shipped[]' value='1'>"; echo "</td><td>"; echo '<input type="button" name="print" value="Print" onclick="document.location.href=\'create_pdf_shipping_label.php\'">'; echo "</td></tr>"; } echo "<tr>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th></th>"; echo "<th> <form action='update.php' method='POST'> <input name='sent' value='shipped[]' type='hidden'> <input type='submit' name='Submit' value='Submit'> </th>"; echo "<th></th>"; echo "</tr>"; echo "</table>";
echo "<br>"; $num_rows = mysql_num_rows($result); echo "$num_rows samples are awaiting shipment.<br>"; echo "Updated on: ".date("l, F d, T h:i" ,time()); echo "<br>";
?> update.php
<?php
error_reporting(E_ALL|E_STRICT);
require_once("connections/connection.php"); // Connection to the server
$tbl_name="sample_requests"; // Table name
if (isset($_POST['Submit']) && $_POST['Submit'] == 'Submit') { $requestid = ($_POST['sent']);
$sql = mysql_query("UPDATE $tbl_name SET sent = 1 WHERE request = '$requestid' "); $result = mysql_query($sql); }
header("location:view_sample_requests.php");
exit; ?> Thank you in advance.
|
coopster

msg:4507992 | 7:11 pm on Oct 14, 2012 (gmt 0) | Have you dumped out that information yet? You are going to see that the "sent" index is an array and you will need to use it accordingly. See this line here ...
$requestid = ($_POST['sent']); ... for more information. Also, NEVER use user-supplied data in a MySQL query unless you have determined it contains exactly what you expect and nothing more. And at the very least you should use the mysql_real_escape_string() method on that data. More reading here: [php.net...] [php.net...]
|
vincevincevince

msg:4514512 | 5:40 am on Nov 1, 2012 (gmt 0) | The issue you have is here:
echo "<input type='checkbox' name='shipped[]' value='1'>"; ... <input name='sent' value='shipped[]' type='hidden'>
First, the value for those 'shipped' entries in the first quoted line need to be 'request' numbers from your database. Second, the other quoted line is actually in the form (the other stuff is outside the form and useless)... but it sets the value of 'sent' to 'shipped'! What you need to be doing is ignoring 'sent', and using 'shipped[]', and in your update, looping through those, something like:
foreach ($_POST['shipped'] as $k=>$v) { if (intval($v)) ...update request $v... }
That approach works, but I prefer to do it using:
echo "<input type='checkbox' name='shipped[$request]' value='1'>"; ... and then ... foreach ($_POST['shipped'] as $k=>$v) { if (intval($k)&&$v==1) ...update request $k... }
|
|
|