mythri Posted May 14, 2014 Share Posted May 14, 2014 Hello, I am trying to update my database with multiple array values like this. if (isset($_POST['submit_multiple'])) { $id=$_POST['selector']; $class=$_POST['class']; $section=$_POST['section']; $N = count($id); for($i=0;$i<$N;$i++) { echo "update table1 set transfer_status='yes',transfer_date='".date('Y-m-d')."',class='".$class[$i]."',section='".$section[$i]."' where enroll_no='".$id[$i]."'"; } and in the form <form action="transfer_mul_student.php" method="post"><select name="class[]"> <option value="">--SELECT CLASS--</option> <option value="Nursery">Nursery</option> <option value="LKG">LKG</option> <option value="UKG">UKG</option> <option value="I">I</option> <option value="II">II</option> <option value="III">III</option> <option value="IV">IV</option> </select> <select name="section[]"><option value="">--SELECT SECTION--</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> </select><input name="selector[]" id="selector" type="checkbox" value="<?php echo $row['enroll_no']; ?>" /> <input type="submit" class="delete_multiple" value="Transer" name="submit_multiple" /> Here $row['enroll_no'] is from table student.I have attached the screenshot for the page display with form , In that If i select checkbox1, checkbox2 checkbox3.. it takes the value for class and section properly, but if i select checkbox1, checkbox3, it takes value for 1st record properly and for 2nd one it takes blank value for class and section. How to overcome this? Please suggest Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/ Share on other sites More sharing options...
mythri Posted May 14, 2014 Author Share Posted May 14, 2014 Please someone help me in this I really need your help Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479464 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 If the second checkbox is not checked it is not posted, so the third checkbox will be the second in your POST array and no longer corresponds to the correct items in the other arrays (which are always posted) I would use the enroll_no to tie them together eg <select name='class[enroll_no]' > <select name='section[enroll_no]' > <input name="selector[enroll_no]" ... > Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479466 Share on other sites More sharing options...
mythri Posted May 14, 2014 Author Share Posted May 14, 2014 Hello, I have used like this <select name="class[<?php echo $row['enroll_no']?>]"> It is taking class with roll_no . But if i display $class= $_POST['class']; print_r ($class); It displays unselected checkbox value also something like this Array ( [2014001] => VIII [2014002] => [2014003] => XII ) I have selected only 1 and 3 Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479469 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 (edited) Exactly! I said the other inputs (class and section) would always be posted. Try this to see the whole POST echo '<pre>', print_r($_POST, 1), '</pre>'; Edited May 14, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479470 Share on other sites More sharing options...
mythri Posted May 14, 2014 Author Share Posted May 14, 2014 Noooooo It takes all the data (including unselected) Result is like this Array ( [class] => Array ( [2014001] => II [2014002] => [2014003] => XI ) [section] => Array ( [2014001] => B [2014002] => [2014003] => B ) [selector] => Array ( [0] => 2014001 [1] => 2014003 ) [update_multiple] => Transer ) Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479474 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 (edited) That is what I keep telling you. Notice you only have 2 items in the "selector" array. Those are the two you should process. foreach ($_POST['selector'] as $rollno) { $class = $_POST['class'][$rollno]; $section = $_POST['section'][$rollno]; echo "update table1 set transfer_status='yes' ,transfer_date=CURDATE() ,class='$class' ,section='$section' where enroll_no=$rollno"; } PS Don't forget to sanitize the POST data before using it in the query Edited May 14, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479475 Share on other sites More sharing options...
mythri Posted May 14, 2014 Author Share Posted May 14, 2014 Heyyyyyy!! Thanks a ton!. This worked for me!. Thank you sooo much Quote Link to comment https://forums.phpfreaks.com/topic/288485-updating-multiple-array-daya-with-selection-and-checkbox-values/#findComment-1479481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.