0

我已经编写了一些 php 代码来以表的形式从 mysql 数据库中检索一些记录。该表有三列,其中前两列是从数据库中填充的,第三列是每行的下拉列表。现在我想从填充的行中选择下拉列表,并将每行的信息存储在一个新的数据库中。

请查看代码并回复您的建议。

<html>
    <?
        // data from the "recordentry.php";
        $date = $_POST['date'];
        $course = $_POST['course'];
        $period = $_POST['period'];
        $batch = $_POST['batch'];
        $subject = $_POST['subject'];
        $faculty = $_POST['faculty'];
    ?>
    <p> Current Date&Time:<? echo $date ?></p>
    <form enctype="multipart/form-data" name="f1" method="POST" ACTION="<?=$self ?>"">
    <?
        $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());
        $num_rows = mysql_num_rows($result);
        echo "<table border='1' align='center'><tr><th>Name</th><th>Unique Identification Code</th>
    <th>Attendance</th></tr>";
        while ($row = mysql_fetch_array($result))
        {
            //Display the results in different cells
            echo "<tr><td align=center>" . $row['name'] . "</td><td align=center>" . $row['uic'] . "</td><td align=center><select name='attendance' style='background-color:#FFC'>
    <option>Present
    <option>Absent
    </select></td></tr>";
            $data[] = array(
                'name' => $row['name'],
                'uic' => $row['uic'],
                'attendance' => $row['attendance']
            );
        }
        echo "<tr><td><input type='submit' value='Submit' name='submit_button' />";
        echo "</table>";
        foreach ($data as $value)
        {
            $name = mysql_result($value['name']);
            $uic = mysql_result($value['uic']);
            $a_status = mysql_result($value['attendance']);
            $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);
        }
    ?>
    </form>
</html>

上面的代码可以从数据库中所有检索到的数据中填充表。但是提交后它不能存储最后三个字段$name、$uic 和$attendance。所以,请帮助我。

4

1 回答 1

0

尝试这个:

 $name = mysql_result($result, $value['name']);  
 $uic = mysql_result($result, $value['uic']);  
 $a_status = mysql_result($result, $value['attendance']);  

这将获得一次值- 但您在该查询中重用 $result (当您插入时),因此它将被覆盖。更改第一个呼叫或第二个呼叫。

于 2012-12-16T13:12:50.827 回答