我在学校有一间教室,当我点击某个教室时,我想将学生添加到其中,但我的实际代码却做了一些愚蠢的事情。它增加了一个学生,但我可以看到所有教室里的学生,而不仅仅是我加入的学生。因此,当我在1号教室,我看到一个表格,我可以在那里添加一个学生,...看看它是如何工作的:
这是代码:http://www.xxxx.xx/projekt/
这是我在trya.php文件中的代码
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb"><a href="zmazat_studenta.php?id_student=<? echo $student['id_student']; ?>">Zmazať</a></td>
</tr></table>
<?php
}
?>
这里是vlozit2.php(适用于添加学生的表单的代码)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
答案 0 :(得分:0)
在你的vlozit2.php
文件中,没有关于插入类ID的信息。所以把
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
到您的表单并在vlozit2.php
中从$_POST['classId']
获取此值,并将其与其他学生数据或您想要的任何地方一起插入。
答案 1 :(得分:0)
尝试用这些snipets替换你的部分代码:
1)在triala.php中
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2)在vlozit2.php
中$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
和
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
希望您将id_trieda存储为INT类型。