Jump to content

Multiple insertion with checkbox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
liri ayekpam

liri ayekpam

    Newbie

  • Members
  • Pip
  • 8 posts
I have a database table with program details. It has program ID (proid), program name (pronm) and others. I have selected all the row from the program table using a while loop and associated a checkbox with each program. The following is the code.
<?php
	include('connect.php');
	session_start();
	$rec=mysql_query("select * from prog");
    $t=0; 
 print"<form method=post action=rating_insert.php>";
 print "<table width=450 border=0 cellspacing=0 cellpadding=3>";
 while($rec_row=mysql_fetch_array($rec))
	{
	     $t=$t+1;
		 //display the result in two column
		 print "<tr><td><input name=chck[] type=checkbox value=$rec_row[proid]/></td><td colspan=3>$rec_row[pronm]</td>";
		  if($rec_row=mysql_fetch_array($rec))
		  {
		  $i+1;
		  print"<td><input name=chck[] type=checkbox value=$rec_row[proid] /></td><td colspan=3>$rec_row[pronm]</td></tr>";
		  $t=$t+1;
		  }
 	}
	print "<tr><td></td><td></td><td align=right><input name=submit type=submit value='Submit' /></td></tr>";
	print "</table>";
	print "</form>";
	//stores the number of result
	$_SESSION['no']=$t;
      
?>


Now what I want to do is, I want to insert the program ID of those programs which the user select through checkboxes to another table. I have tried it but it is not working so I won't post the code. Need Help..
Any help is highly appreciated...

Edited by WingedPanther, 09 October 2009 - 01:05 PM.
add code tags (the PHP sheet)


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
at first, please use apostrophe or quotation marks around your html attributes, that's how it should be done...

but what you would have to do is:
foreach ((array) $_POST['chck'] as $chk) {
  //whatever you want to do with each value
}
the (array) typecasts the variable to always be an array, even if it's empty. otherwise, this will fail if no checkboxes is checked.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
liri ayekpam

liri ayekpam

    Newbie

  • Members
  • Pip
  • 8 posts
Many thanks Orjan. The code works.