td value to mysql
im new in php! i currently display data from MySQL every time i search for name or idnumber and display all in to the html table but when i click the button on the table row i get the last value of the data.
here is the code i made:
$searchdata=mysql_query("select * from tblstudent where Last_name
='".$search."'")or die("Error Query");
if(isset($_POST['Search'])){
if(empty($search)){
$error_2="Search Box is empty";
}
elseif(strlen($search)<1){
$error_2="ERROR SEARCH";
}
else{
//display data to the table
echo "<div id='res'><form id='form1' method='POST' action=".basename(__FILE__).">
Reservation Fees:<input id='descriptive' name='reserv' type='text' /> ";
echo "<table id='example' border='1'>
<tr>
<th>ID NUMBER</th>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>MIDDLE NAME</th>
<th>COURSE</th>
<th>GENDER</th>
</tr>";
while($row=mysql_fetch_array($searchdata)){
$studentid=$row['student_id'];
$coursee=$row['Course'];
$lnamee=$row['Last_name'];
$fnamee=$row['First_name'];
$mnamee=$row['M_name'];
开发者_Go百科$gnder=$row['Gender'];
echo "
<tr>
<td>".$studentid." <input type='hidden' name='dummyID' value='$studentid'></td>
<td>".$lnamee." <input type='hidden' name='dummylname' value='$lnamee'></td>
<td>".$fnamee."<input type='hidden' name='dummyfname' value='$fnamee'></td>
<td>".$mnamee."<input type='hidden' name='dummymname' value='$mnamee'></td>
<td>".$coursee."<input type='hidden' name='dummycourse' value='$coursee'></td>
<td>".$gnder."<input type='hidden' name='dummygnder' value='$gnder'></td>
<td><input id='send' name='add' type='submit' value='Reserve' /></td>
</tr>";
}
echo "</table>
</form></div>";
}}}
you have a form with multiple elements named dummyID, dummylname, etc. So the value that gets submitted is just the last one you output.
Add in an index {$i} to determine which row is being submitted.
$i = 0;
while($row=mysql_fetch_array($searchdata)){
$studentid=$row['student_id'];
$coursee=$row['Course'];
$lnamee=$row['Last_name'];
$fnamee=$row['First_name'];
$mnamee=$row['M_name'];
$gnder=$row['Gender'];
echo "
<tr>
<td>".$studentid." <input type='hidden' name='dummyID[$i]' value='$studentid'></td>
<td>".$lnamee." <input type='hidden' name='dummylname[$i]' value='$lnamee'></td>
<td>".$fnamee."<input type='hidden' name='dummyfname[$i]' value='$fnamee'></td>
<td>".$mnamee."<input type='hidden' name='dummymname[$i]' value='$mnamee'></td>
<td>".$coursee."<input type='hidden' name='dummycourse[$i]' value='$coursee'></td>
<td>".$gnder."<input type='hidden' name='dummygnder[$i]' value='$gnder'></td>
<td><input id='send' name='add[$i]' type='submit' value='Reserve' /></td>
</tr>";
$i++
}
Then process the data like:
if(!empty($_POST['add'])) {
$i = current(array_keys($_POST['add']));
$studentid=$_POST['dummyID'][$i]
$coursee=$_POST['dummycourse'][$i];
$lnamee=$_POST['dummylname'][$i];
$fnamee=$_POST['dummyfname'][$i];
$mnamee=$_POST['dummymname'][$i];
$gnder=$_POST['dummygnder'][$i];
}
精彩评论