I want to take students attendance.
i had made a page attendance.php
This is coding part

    <?php
$report = mysql_query("SELECT id, name, rollno FROM registered_members") or die(mysql_error());
?>
<form action="attendinsert.php" method="post">
<table id = "attendance" width="567" border="1 bold">
  <tr>
    <th width="83" scope="col">ID</th>
    <th width="83" scope="col">Student Name</th>
    <th width="55" scope="col">Student Roll.No</th>
    <th width="51" scope="col">Attendance</th>

  </tr>
  <?php while(list($id, $name, $rollno) = mysql_fetch_row($report))
  {
  ?>
  <tr>
    <td><?php echo $id ?></td>
    <td><?php echo $name ?></td>
    <td><?php echo $rollno ?></td>
    <td align="center"><?php echo '<input type="hidden" name="att[]" value="0"/>';?><?php echo '<input type="checkbox" checked="checked"  name="att[]" value="1"  />'; ?></td>
    <input type="hidden" name="rollno[]" value="<?php echo $rollno; ?>"  />
  </tr>
<?php
  }
  echo '</table>';
  ?>
  <input type="submit" name ="submit2"  id="submit2" value ="submit"></input>
</form>




  ///////////////////////////////////////////////////////



  And the second page is attendinsert.php
  by which i want to save data in attendance table 





<?php
$att = $_POST['att'];
$rollno = $_POST['rollno'];
            foreach($att as $key => $attendance) {
                          $at = $attendance ? '1' : '0';
                $query = "INSERT INTO `attendance`(`rollno`,`att`) VALUES ('".$rollno[$key]."','".$at."') ";
                $result = mysql_query($query);
            }           
?>

I dont know whats the error. on line

 $query = "INSERT INTO `attendance`(`rollno`,`att`) VALUES ('".$rollno[$key]."','".$at."') ";
Member Avatar for diafol

A few things - DO NOT place input variables (e.g. POST) directly into your query and stop using mysql_* functions - they're deprecated. Move over to PDO or mysqli.

You're also running multiple sql queries whne you only need to run ONE. You can build up a VALUES clause in the loop, to get something like...

INSERT INTO `attendance`(`rollno`,`att`) VALUES (16347,1),(1837,0),(347,1),(36587,0),(748,1)

Thanks for reply Diafol. But i dont know how to do that.
will you please guide me step by step.
i think you have an idea in your mind how to solve this problem.
Plese help me...

Member Avatar for diafol

Seems like this is a homework / coursework task as it's very popular. :(
However, here's an freebie - a way to get the data you need into a values clause and a bind parameters array - ready for use with PDO (or mysqli).

$valuesClause = susbtr(str_repeat('(?,?),',count($att)),0,-1);
$bindArray = array();

foreach($att as $key => $attendance) {
    $at = ($attendance) ? '1' : '0';
    $rollkey = $rollno[$key];
    $bindArray = array_merge($bindArray, array($rollkey,$at));
}    

If you don't know about PDO, you can read up on it at php.net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.