php - 如何为行添加值?

标签 php html mysql

我正在尝试创建类(class)记录。我创建了一个表,其中包含名称和事件列。

表格看起来像这样。

Name   |    Q1    |    Q2      |
--------------------------------
Prinz  |    20    | (input text)

My aim is to save the input data into my database, but I need to have the id of the student(Prinz). I already managed to save the grade but want the id of that particular student to be save as well. My MYSQL table has 4 att. namely, grade_id(auto_inc), stud_id, grade and the activity type(Quiz, Assignment, Exam).

Here's my code

<table border="0" cellspacing="0" cellpadding="0" style="width: 845px; margin-top: 20px; margin-bottom: 65px;">
            <tr style="background-color: #caccca; text-align: center;">
            <td style="width: 130px;" class="td-1">NAME</td>
            <?php 
            $result = mysql_query("select * from subject_grade ");
            $numrows = mysql_num_rows($result);
            while ($row = mysql_fetch_object($result)) {
            $act_type = $row->act_type;
            echo '<td style="width: 130px;" class="td-1">'.$row->act_type .'</td>';
            }
            ?>
            <td class="td-1">GPA</td>
            </tr>
            <?php 

            $result = mysql_query("select * from student_db ");
            while ($row = mysql_fetch_object($result)) {
            echo '<tr class="border" style="background-color: #f9f9f9;"><td style="width: 150px;" class="td-2" name="student"><p>'.$row->firstname .' '. $row -> lastname .'</p></td>';

                $id = $row->student_id;
                for ($i=0; $i<$numrows; $i++){
                    echo '<form action ="record.php" method="POST"><td style="text-align: center;"><input type="text "style="font-size: 12px; width: 20px;" name="grade"><input style="display: none;" type="text" name="id" value="'.$id.'"><input type="submit"></td></form>';
                }
            echo '<td style="width: 80px;"class="td-2"></td>';
            }

            ?>

            </tr>
        </table>

RECORD.PHP 文件

if($_POST['grade']){
        $sql="INSERT INTO class_record (grade, student_id)
        VALUES
        ('$_POST[grade]','$_POST[id]')";

        $rs1 = mysqli_query($con,$sql);
        echo $_POST['grade'] .' '. $studID;

} else echo 'ERROR';

?>

我希望你能帮助我,这是我们学校的项目。

最佳答案

请更改您的查询,您的查询有误, 改变

$sql="INSERT INTO class_record (grade, student_id)
        VALUES
        ('$_POST[grade]','$_POST[id]')";

$sql="INSERT INTO class_record (grade, student_id)
        VALUES
        ('".$_POST['grade']."','".$_POST['id']."')";

关于php - 如何为行添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348285/

相关文章:

php - 如何在ajax表单提交后清除表单字段并打印成功消息

javascript - 使用javascript替换带有连字符的字符串

php - 在不同于 Ubuntu/Windows 的 Mac 上运行 PHP & MySQL

html - Google 字体粗细 300 不起作用

html - 默认单选按钮已选中,但它的样式不起作用

MySQL Roles.RemoveUserFromRole 在 MySQL 中不起作用

java - 插入 "Duplicate entry ... for key PRIMARY"和 "Krakow"时为 "Kraków"

php - 是否可以从 MySQL 数据库加载坐标并在 Android 谷歌地图中显示为标记

javascript - AJAX不会第二次获取JSON数据

javascript - 如何定义多个位置 使用 Google Maps API 为循环生成的帖子列表中的每个帖子放置图钉