1981tarun Posted September 12, 2010 Share Posted September 12, 2010 hello friend, I use these code for update my database with radio button checked or unchecked through array but my code could not update the server data please help me ::: my code is ::: <?php //include configuration file for connection include_once('config.php'); $sql = "select * from ATTENDANCE"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> <table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px"> <tbody> <tr> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student ID</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Name</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Present</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Absent</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Comment</th> </tr> <?php while($rows=mysql_fetch_array($result)) { ?> <tr> <td class="table1"> <? $id[] = $rows['STUDENT_ID']; ?><? echo $rows['STUDENT_ID']; ?> </td> <td class="table1"> <? echo $rows['STUDENT_NAME']; ?> </td> <td id="present"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" checked="checked" value="PRESENT">Present </td> <td id="absent"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" value="ABSENT">Absent </td> <td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"> <? echo $rows['COMMENT']; ?> </td> </tr> <?php }?> <tr> <td colspan="5" style="vertical-align:middle; text-align: center;"><br><br> <input id="Submit" type="submit" name="Submit" value="Submit" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;"> </td> </tr> </tbody> </table> </form> <?php if($_POST['$Submit']) { foreach($_POST['STUDENT_ID'] as $id) { $sql = "UPDATE ATTENDANCE SET STUDENT_PRESENT = '".$_POST["present".$id]."' WHERE STUDENT_ID = '".$id."' "; $result = mysql_query($sql); } } if($result) { print_r ($_POST); header("location:report.php"); } else { echo "Your entry is not completed at this time............."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213209-updating-multiple-rows-in-mysql-with-radio-button/ Share on other sites More sharing options...
MadTechie Posted September 12, 2010 Share Posted September 12, 2010 1. use PHP tags 2. move the update code above the select code 3. for debugging update both mysql_query($sql); to mysql_query($sql) or die($sql.mysql_error()); 4. report errors if any (after you applied the updates (3) Quote Link to comment https://forums.phpfreaks.com/topic/213209-updating-multiple-rows-in-mysql-with-radio-button/#findComment-1110189 Share on other sites More sharing options...
taquitosensei Posted September 12, 2010 Share Posted September 12, 2010 There's several problems here. foreach($_POST['STUDENT_ID'] as $id) yet you don't have this field any where. all you have is this <td id="present"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" checked="checked" value="PRESENT">Present </td> <td id="absent"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" value="ABSENT">Absent </td> to do it the way you're trying to do you'd have to do a form for each student so you'd have to move your <form> and submit button inside the loop. But then you'd be submitting each student individually. You could try this //include configuration file for connection include_once('config.php'); $sql = "select * from ATTENDANCE"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> <table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px"> <tbody> <tr> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student ID</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Name</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Present</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Absent</th> <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Comment</th> </tr> <?php while($rows=mysql_fetch_array($result)) { ?> <tr> <td class="table1"> <? $id[] = $rows['STUDENT_ID']; ?><? echo $rows['STUDENT_ID']; ?> </td> <td class="table1"> <? echo $rows['STUDENT_NAME']; ?> </td> <td id="present"> <input type="radio" name="<? echo $rows['STUDENT_ID']; ?>" checked="checked" value="PRESENT">Present </td> <td id="absent"> <input type="radio" name="<? echo $rows['STUDENT_ID']; ?>" value="ABSENT">Absent </td> <td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"> <? echo $rows['COMMENT']; ?> </td> </tr> <?php }?> <tr> <td colspan="5" style="vertical-align:middle; text-align: center;"><br><br> <input id="Submit" type="submit" name="Submit" value="Submit" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;"> </td> </tr> </tbody> </table> </form> <?php if($_POST['$Submit']) { foreach($_POST as $k=>$v) { if($k!="Submit") { $sql = "UPDATE ATTENDANCE SET STUDENT_PRESENT = '".$v."' WHERE STUDENT_ID = '".$id."' "; $result = mysql_query($sql); } } } if($result) { print_r ($_POST); header("location:report.php"); } else { echo "Your entry is not completed at this time............."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213209-updating-multiple-rows-in-mysql-with-radio-button/#findComment-1110190 Share on other sites More sharing options...
sasa Posted September 12, 2010 Share Posted September 12, 2010 change this part of code <td id="present"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" checked="checked" value="PRESENT">Present </td> <td id="absent"> <input type="radio" name="present<? echo $rows['STUDENT_ID']; ?>" value="ABSENT">Absent </td> to <td id="present"> <input type="radio" name="present[<? echo $rows['STUDENT_ID']; ?>]" checked="checked" value="PRESENT">Present </td> <td id="absent"> <input type="radio" name="present[<? echo $rows['STUDENT_ID']; ?>]" value="ABSENT">Absent </td> and in action page yo can do foreach(S_POST['present'] as $student_id => $value){ //etc. } Quote Link to comment https://forums.phpfreaks.com/topic/213209-updating-multiple-rows-in-mysql-with-radio-button/#findComment-1110194 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.