Forum Moderators: coopster

Message Too Old, No Replies

inserting multiple rows into mysql with checkbox

         

rayofshadow

1:03 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



Hi All.. i have problem with my script, to input data from multiple check box into mysql.

when i submit, input id is right, but input description is not following the id

here the code:


<?php
// conect to database & tabel here

//process input form
if(isset($_POST[op])== "1"){

$item = $_POST['id'];
$description = $_POST['description'];

$num = count($item);
$numb = $num-1;
for ($i=0; $i<=$numb; $i++)
{

$items = $item[$i];
$desc = $description[$i];

$query = "INSERT INTO tablename (ItemNo, Description)
VALUES ('$items','$desc')";
mysql_query($query) or die(mysql_error());

}
} else {
?>
<form name="input" method="post" action="">
<input type="hidden" name="op" value="1" />
<table width="337">
<th width="91"><div align="left">Item</div></th>
<th width="234"><div align="left">Description</div></th>
<?
$number = 3;
for ($i=1; $i<=$number; $i++)
{?>
<tr>
<td><input type="checkbox" name="id[]" value="red<?=$i?>" />product<?=$i?></td>
<td> <input type="text" name="description[]" size="10" value="description-<?=$i?>"> </td>
</tr>
<?php }
?>
</table>
<input type="submit" name="submit" value="Submit">

</form>
<?php }?>

thanks for your help

Habtom

1:20 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should probably work the way it is, try the following:

$item = $_POST['id'];
$description_2 = $_POST['description'];

$num = count($item);
$numb = $num-1;
for ($i=0; $i<=$numb; $i++)
{

$items = $item[$i];
$desc = $description_2[$i];

Habtom

rayofshadow

1:37 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



no its not working, there is no different between your code and mine.

try to test with check product1 and product1, and look at your table, you will see if this is not running.

Habtom

1:47 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> try to test with check product1 and product1, and look at your table, you will see if this is not running.

I couldn't get what you meant by that.

rayofshadow

1:55 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



I mean:
if you running that script, you will got 3 list, here the list:

(number 1,2,3 is checkbox)

Item Description
1. product1 - Description1
2. product2 - Description2
3. product3 - Description3

when we check 1 and 3, and then submit, its will save wrong data (not the right data that i want)

here the data save to table when we check 1 and 3:

ItemNo Description
--------------------------
product1 Description1
product3 Description2

see.. the product 3 description is not the right data, it should be Description3 not Description2

can you help me pls.
thanks

Habtom

2:01 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alright, when you mentioned it is not working, I assumed it wasn't displaying it at all gggggg

Note the changes:

$num = count($item);
for ($i=1; $i<=$num; $i++)
{

and on this block as well:

<td><input type="checkbox" name="id[<?=$i?>]" value="red<?=$i?>" />product<?=$i?></td>
<td> <input type="text" name="description[<?=$i?>]" size="10" value="description-<?=$i?>"> </td>

Habtom

rayofshadow

2:12 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



Still not running.
but thankyou verymuch for your help.

rayofshadow

4:07 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



Aha got the solution
i change for(...) with foreach(...)

if someone have trouble same like me, please ask here, maybe i can help you.

thanks