mattennant Posted November 27, 2007 Share Posted November 27, 2007 Hi there. I'm looking to select using checkboxes certain rows from the results of 'table a' and add the selected rows to another table using checkboxes. I Have had limited success adapting some code for deleting multiple records using checkboxes. But to be honest got a bit stuck At the moment i check one check box one row is added, but not the one i checked Here's what i have so far <?php $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 } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> .......... <?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++){ if($checkbox[$i] == 'y'){ $sql1="INSERT INTO $tbl_name2 (name, lastname, email)VALUES('$name[$i]', '$lastname[$i]', '$email[$i]')"; $result1=mysql_query($sql1); } } } if($result1){ header("location:insert_multiple.php"); } mysql_close(); ?> 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 Link to comment https://forums.phpfreaks.com/topic/79103-solved-adding-multiple-rows-to-table-using-checkboxes/ Share on other sites More sharing options...
mattennant Posted November 27, 2007 Author Share Posted November 27, 2007 hi this is now solved in case anyone is interested here is the solution <?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 the 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]}')"; cool Link to comment https://forums.phpfreaks.com/topic/79103-solved-adding-multiple-rows-to-table-using-checkboxes/#findComment-400387 Share on other sites More sharing options...
nsandberg Posted July 22, 2010 Share Posted July 22, 2010 This was exactly what I was looking for and now I have my application working, but I am running into a small issue with the loop. When I select more than a single checkbox, only the last checkbox is inserted into the table. Why? $i = 0; while($students=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $i++; ?>" /></td> <td align="center"><input name="lastName[]" type="text" id="lastName" value="<? echo $students['lastName']; ?>"></td> <td align="center"><input name="firstName[]" type="text" id="firstName" value="<? echo $students['firstName']; ?>"></td> <td align="center"><input name="AP[]" type="text" id="AP" value="<? echo $students['AP']; ?>"></td> <td align="center"><input name="last_teacher[]" type="text" id="last_teacher" value="<? echo $students['last_teacher']; ?>"></td> <br /> </tr> <?php } ?> <tr> <!-- <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> --> <?php // Get values from form $lastName=$_POST['lastName']; $firstName=$_POST['firstName']; $AP = $_POST['AP']; $last_teacher = $_POST['last_teacher']; $description = $_POST['description']; $subject = $_POST['subject']; $checkbox=$_POST['checkbox']; // Check if button name "Submit" is active, do this if($_POST['Submit']){ foreach($checkbox as $i){ $sql2 = "INSERT INTO assignments SET firstName = '$firstName[$i]', lastName = '$lastName[$i]', AP = '$AP[$i]', last_teacher = '$last_teacher[$i]', description = '$description', subject = '$subject'"; } $result2=mysql_query($sql2) or die(mysql_error()); if($result2){ header("location:../index.php"); } mysql_close(); } ?> Link to comment https://forums.phpfreaks.com/topic/79103-solved-adding-multiple-rows-to-table-using-checkboxes/#findComment-1089805 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.