我正在尝试创建类(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/