How To Insert Data From Checkbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sphinx_vii
Forum Newbie
Posts: 2
Joined: Sat Apr 19, 2008 6:05 am

How To Insert Data From Checkbox

Post by sphinx_vii »

heLLooo..i'm doing a system that will auto assign a person to a company based on state...i will choose few person using a checkbox that hold the id and insert the data based on the state..for example there's 4 company in the same state and i want to assign 2 person..the problem is that i cant find a solution on how to assign the 2 person into 4 places..the codes below will only assign 1 person as it will took the last data from the array..could anyone give me the logic or ideas for this??....


<td bgcolor=lightblue align="center"><input name="checkbox[]" type="checkbox" value="$log_id" checked)/></td>

******************************************************************************************
if(isset($_POST['checkbox']))
{
$checkbox = $_POST['checkbox'];
$n = count($checkbox);
$i = 0;

while ($i < $n)
{
echo "<center><li>$checkbox[$i]</li> \r\n</center>";
$check=$checkbox[$i];
$i++;

$query = "UPDATE auto_assign SET lect_id='$check' WHERE co_state='$co_state'";
mysql_query($query) or die('Error, query failed');

}
echo "</ol></tr></td>";

*****************************************************************************************
the database....(supposed it will insert the id into the data)

id co_name co_state
0 Fellisa Co Jordan
0 Mine Jordan
0 Abraham co Egypt
0 Dancom Jordan
0 Wowo Co Andala
0 Marcom Andala
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: How To Insert Data From Checkbox

Post by aceconcepts »

What you want to do is determine the amount of people being added and the amount of companies they're being added to.

then loop the people qty and within that loop, loop the company qty and add each person to each company.
sphinx_vii
Forum Newbie
Posts: 2
Joined: Sat Apr 19, 2008 6:05 am

Re: How To Insert Data From Checkbox

Post by sphinx_vii »

oritee...got it done!!...thanxx...

***************************************************************

if(isset($_POST['checkbox']))
{
$checkbox = $_POST['checkbox'];
$n = count($checkbox);
$i = 0;

$resulta = mysql_query("SELECT assign_id, co_state, co_name FROM auto_assign WHERE co_state='$co_state' GROUP BY co_name") or die("Invalid Query: " .mysql_query());

$numrow = mysql_fetch_row(mysql_query("SELECT count(co_name) FROM auto_assign WHERE co_state='$co_state' GROUP BY co_name"));

while ($i < $n)
{
while ($row = mysql_fetch_assoc($resulta))
{
$assign_id = $row['assign_id'];
$co_state = $row['co_state'];
$co_name = $row['co_name'];


echo "<center><li>$checkbox[$i]</li> \r\n</center>";
$check=$checkbox[$i];

$query = "UPDATE auto_assign SET lect_id='$check' WHERE co_name='$co_name' AND co_state='$co_state'";
mysql_query($query) or die('Error, query failed');
$i++;

};
}
Post Reply