将行数组存储到mysql数据库中

时间:2012-12-15 11:03:44

标签: php

我已经编写了一些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。所以,请帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个:

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

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