We help IT Professionals succeed at work.

PHP / HTML Form Processing

Bryan_123
Bryan_123 asked
on
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>
<td width="10">
<input type="hidden" name="Citation_Pic[]"><img src="'.$row['Citation_Pic'].'">
 </td>
 <td width="430">
 <input type="hidden" name="Name[]" value="'.$row['Name'].'"/>
 '.$row['Name'].'
</td>

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";
 }

}
Comment
Watch Question

CERTIFIED EXPERT
Commented:
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.

Author

Commented:
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.