Forum Moderators: coopster

Message Too Old, No Replies

Inserting multiple rows to a table using checkboxes

         

mattennant

3:20 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



Hi there. I'm looking to select certain rows from one table and add the selected rows to another table using checkboxes. I Have had limited success adapting some code for deleting multiple records using checkboxes.

At the moment all the records are added even if i only check one checkbox, which is progress but not exactly what i'm looking for.

Here's what i have so far


<?
$host="localhost"; // Host name
$username="UN"; // Mysql username
$password="PW"; // Mysql password
$db_name="DB"; // Database name
$tbl_name="test_mysql"; // Table name
$tbl_name2="test_mysql2"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);

<?php
while($rows=mysql_fetch_array($result)){
?>
......
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="y" /></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name'];?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname'];?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email'];?>"></td>
</tr>
.............
<?php
}
?>
<input type="submit" name="Submit" value="Submit">

<?php

// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Check if button name "Submit" is active, do this
if($checkbox){
for($i=0;$i<$count;$i++){

$sql1="INSERT INTO $tbl_name2 (name, lastname, email)VALUES('$name[$i]', '$lastname[$i]', '$email[$i]')";
$result1=mysql_query($sql1);
}
}

i realise i somehow have to get the selected checkboxes into the loop, but have drawn a bit of a blank.

Any help much appreciated

Mat

cameraman

4:58 pm on Nov 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that unchecked checkboxes don't appear in the array. In your example, every checkbox you see in $_POST['checkbox'] will = 'y'.

So you have to keep the checkboxes synced with the data.
<?php
$i = 0;
while($rows=mysql_fetch_array($result)){
?>
......
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $i++;?>" /></td>

Then for your insert loop:
foreach($_POST['checkbox'] as $i) {
$sql1="INSERT INTO $tbl_name2 (name, lastname, email)VALUES('{$_POST['name'][$i]}', '{$_POST['lastname'][$i]}', '{$_POST['email'][$i]}')";

What this is doing is assigning unique values to the checkboxes that correspond to the automatically generated indices of the other form fields. There's at least a couple of other ways to do it but this should work well with the way you've laid it out. If you want to clear up the validation errors, change the id to be "checkbox<?php echo $i;?>" so that each will have a unique id.

mattennant

5:16 pm on Nov 27, 2007 (gmt 0)

10+ Year Member



Thanks so much that worked like a dream. Brilliant!