We help IT Professionals succeed at work.

PHP / HTML Form Processing

Bryan_123 asked
I have a form that queries a mysql database and prepopulates each <td> through a while($row = mysql_fetch_array( $result )) loop.

10 items are populated.

The desired behavior is that if the checkbox is checked it will insert those into a mysql database table called 'unit_citations_usa' or if none are checked it will do nothing.

This is the form code:

<td width="10">
<FORM METHOD="POST" ACTION="citation_edit_process.php">
<input type="checkbox" name="Citation_ID[]" value="'.$row['Citation_ID'].'" UNCHECKED/>
<td width="10">
<input type="hidden" name="Citation_Pic[]"><img src="'.$row['Citation_Pic'].'">
 <td width="430">
 <input type="hidden" name="Name[]" value="'.$row['Name'].'"/>

in citation_edit_process i am calling (just practicing with name insert for now) the following, which inserts all 10, not just the ones checked or even if checkboxes are left blank:

foreach($_POST['Name'] as $Citation_Name) {
 if ($Citation_Name !=''){
  mysql_query("INSERT INTO tab_unit_citations_usa (Name) VALUES ('$Citation_Name') ") or die("Your Error " . mysql_error());  
 echo "success";

Watch Question

That's because you're checking $_POST['Name'] in your form for nullness, and it will never be null - you should be looking instead at $Citation_ID's checkbox submission.


Duhhh...I guess I just stared at that one a little too long. Thanks for your help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.