Forum Moderators: open

Message Too Old, No Replies

Populating form from database, then passing results to next page

Looping through checkboxes and displaying whole database rows selected

         

cookie2

3:53 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



I have a multiple select input in a form that's being populated by a row from my database as such:

<input type=\"checkbox\" name=\"subm[]\" value=\"$row[ID]\">

That part is working fine as I can check the displayed page using View Source and see that the value is the correct row number from the database. It is then being submitted on a form by $_POST method to another page where I want to evaluate the checkboxes and display the contents of the entire row that corresponds to each value=\"$row[ID]\" that have been checked. But I can't seem to get it to work. Can someone point me in the right direction?

$query = ("SELECT * FROM `table`");
$result = mysql_query($query);

print "<p>Data for Selections:";
print "<table border=2><tr><th>You chose:";
foreach ($_POST['subm'] as $value) {

print "<tr><td>";
print "$row[ID];\n";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print "</td></tr>";
print "</table>\n";
}
if (!isset($_POST['subm'])){
print "<p>No matching entry
}
mysql_close();

volatilegx

2:05 pm on Apr 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you need to do the query within the foreach loop. do a separate query for each id number.

cookie2

2:03 am on Apr 8, 2006 (gmt 0)

10+ Year Member



Thanks for the reply. What I did was play around with it a little and got some help from an online friend to get it working. In case this may help someone else out, what I did was change the code to:

$ids=implode(",",$_POST['subm']);
$query = ("SELECT * FROM `table` WHERE `ID` IN ($ids)");
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {

and loop through from there. Works fine and I'm told that it's more secure coding, too.