td值为mysql

时间:2011-10-14 18:06:20

标签: php

我是新的PHP!我每次搜索名称或idnumber时都会显示来自MySQL的数据并显示在html表格中,但当我点击表格行上的按钮时,我会得到数据的最后一个值。

这是我制作的代码:

$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'];
                $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>";
}}}

1 个答案:

答案 0 :(得分:1)

你有一个名为dummyID,dummylname等多个元素的表单。所以提交的值只是你输出的最后一个。

添加索引{$ i}以确定正在提交哪一行。

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

}

然后处理数据:

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];
}