0

Actually i want to store values fetched from one database(student info) into other database(attendance) along with selected status of their attendance, i want to store data in every iteration of while loop. The data retrieved from student info database is being successfully stored into attendance database but it does't stores the particular selected option in each iteration, it only stores the value of latest option selected and overwrites previous status of selected option.

<html>
<head>

</head>
<body>

<?php  
error_reporting(E_ALL ^ E_DEPRECATED);
include("config.php");?>
<div class="form-container">
<form method="post" action="" role="form">
 <!-- <div class="container"> -->
 <div class="col-lg-3">
  <div class="form-group">
<?php
  $qs=mysql_query("select * from student_table");
  ?>
  <table border=1>  
  <?php 

  while($stid=mysql_fetch_row($qs))
  {         

  ?>

  <tr>
   <td ><?php echo $stid[0]?></td>
   <td><?php echo $stid[1]?></td>
   <td>
   <select name="present" >
   <option value=""> ---Select Attendence--- </option>
   <option value="P"> Present </option>
   <option value="A"> Absent </option>

   </select></td>

 </tr>

 <?php
      $stud= $stid[0];    //roll no of student from student database
      $subj= $stid[1];    //name of student from student database
      $date = date('Y-m-d H:i:s');    //date
 if(isset($_POST['present'])){     //selected option value, but it gets overwritten and at the end displays latest value except particaular value of every iteration
 $department=$_POST['present'];
      $query=mysql_query("Insert into tbl_attendence  (StudentRollNumber,SubjectId,Attendence,Date)VALUES('$stud','$subj','$department','$date')");

if(!$query)
{
echo mysql_error();
}
}

   }


  ?>
  </table>
  </div>
  </div>   
  <button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>
  </form>

  </body>
  </html>
prog94
  • 11
  • 5
  • Please [stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Also your code is [wide open to SQL injection](http://stackoverflow.com/documentation/php/5828/pdo/2685/preventing-sql-injection-with-parametrized-queries) – Machavity Dec 28 '16 at 18:37

1 Answers1

0

So, what you are ending up with is multiple <Select> elements with the same name. When you do this, only the latest one actually gets posted.

So you need to ensure each element has a unique name. You could do this by making them part of an array, using the student ID as the key:

<select name="present['<?= $stid[0] ?>']" >

Then, when you insert, do something like:

foreach($_POST['present'] as $stud => $present){
    $date = // whatever
    $subj = // whatever    
    $sql="INSERT INTO tbl_attendence (StudentRollNumber,SubjectId,Attendence,Date) VALUES('$stud','$subj','$present','$date')");
}
BizzyBob
  • 12,309
  • 4
  • 27
  • 51
  • Thanx @BizzyBob, i got the concept, but it is giving me this error "Warning: Invalid argument supplied for foreach()" , i am new to php can u please elaborate me and guide me about this warning ? – prog94 Dec 28 '16 at 20:45
  • You will get that if `$_POST['present']` doesn't exist. Wrap the `foreach` in an `if` statement to ensure it is set before using it. – BizzyBob Dec 28 '16 at 20:48