Jump to content

updating multiple rows in mysql with radio button


1981tarun

Recommended Posts

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.............";

}

?>

Link to comment
Share on other sites

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.............";
                        }
                     ?>

 

Link to comment
Share on other sites

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.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.