php code to insert row of data populated from a mysql database into other mysql database after modification
<p>I am trying to make an attendance sheet. In my code I retrieve the names from a existing database and populate in a html table with the attendance select dropdown. After populating the dropdown all the data in the html table automatically saved into the new database without selecting the submit button. moreover the attendance status shows null value in the database, but the other fields are saved successfully. Please go through my code and give your valuable suggestions to rectify the problem.</p>

<pre><code>&lt;form enctype="multipart/form-data" name="f1" method="POST" ACTION="&lt;?php echo $PHP_SELF;?&gt;"&gt;


//call from the previous php page
    &lt;?  $back_page ="recordentry.php";
    $date=$_POST['date'];
$course=$_POST['course'];
$period=$_POST['period'];
$batch=$_POST['batch'];
$subject=$_POST['subject'];
if(empty($_POST['other']))
{
$faculty=$_POST['faculty'];
}
else

{
    $faculty=$_POST['other'];
}
?&gt;
&lt;p&gt;Current Date &amp; Time : &lt;? echo $date ?&gt;&lt;/p&gt;

&lt;?

$db="contact";
//mysql_connect(localhost,$_POST['username'],$_POST['pass']);
$link = mysql_connect("localhost", "root", "");
if (! $link)
die("Couldn't connect to MySQL");

mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());


$result=mysql_query("SELECT name,uic FROM student where batch='$batch' AND course='$course' order by name")
or die("SELECT ERROR: ".mysql_error());
$userinfo = array();
echo "&lt;table border='1' align='center'&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Unique Identification Code&lt;/th&gt;&lt;th&gt;Attendance(select only the Absentees)&lt;/th&gt;&lt;/tr&gt;";

while($row = mysql_fetch_array($result)){
//Display the results in different cells

echo "&lt;tr&gt;&lt;td align=center&gt;" . $row['name'] . "&lt;/td&gt;&lt;td align=center&gt;" . $row['uic'] . "&lt;/td&gt;&lt;td align=center&gt;&lt;select name='attendance' style='background-color:#FFC'&gt;
      &lt;option&gt;Present
       &lt;option&gt;Absent
       &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;";
//$userinfo = array('name' =&gt; $row['name'], 'uic' =&gt; $row['uic'], 'attendance' =&gt; $row['attendance']);
$row_name=$row['name'];
$row_uic=$row['uic'];
$row_attn=mysql_real_escape_string($_POST['attendance']);
$userinfo[] = array("name"=&gt; $row_name , "uic"=&gt; $row_uic, "a_status"=&gt; $row_attn);
}
//echo"&lt;tr&gt;&lt;td&gt;&lt;input type='submit' value='Submit' name='submit_button' /&gt;";
echo"&lt;/table&gt;";

mysql_close($link);
  //form HAS been submitted
?&gt;


&lt;table align="center"&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="Submit" name="submitbtn" &gt;&lt;/td&gt;&lt;td&gt;&lt;input type="reset" value="reset"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;


&lt;/form&gt;

&lt;?
if($_SERVER['REQUEST_METHOD']=='POST')
{

 foreach ($userinfo as $value)

 {
$name = $value[name];  
 $uic = $value[uic];  
 $a_status = $value[a_status];

$db="contact";
$link = mysql_connect("localhost", "root", "");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());

$result=mysql_query("INSERT INTO attendance(date, course, period, batch, subject, faculty, name, uic, attendance) VALUES 
('$date', '$course', '$period', '$batch', '$subject', '$faculty', '$name', '$uic', '$a_status')")
  or die("Insert Error: ".mysql_error());

mysql_close($link);

  }

}
?&gt;
</code></pre>

<p>all the data except attendance save to the database successfully. attendance gives null value. Again, data save before clicking submit button. Please help.</p>
