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

$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">
    <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>

  <?php while(list($id, $name, $rollno) = mysql_fetch_row($report))
    <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; ?>"  />
  echo '</table>';
  <input type="submit" name ="submit2"  id="submit2" value ="submit"></input>


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

$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."') ";
3 Years
Discussion Span
Last Post by 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...


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.