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

}

?>

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)

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.