php - 如何将值从from传递到sql查询
问题描述
我是 php 编程的新手。我有点困惑,我在网上找不到任何有用的信息。我正在尝试从头开始建立一个学校管理系统。我需要的是从数据库中获取所有“提供的课程”并将其放入表格中,并允许学生直接从行中添加课程。我怎样才能做到这一点?
我创建了一个表格和一个输入,学生可以在其中输入课程编号并注册课程。它工作正常。但是我觉得不太实用。
这是我的代码
<?php
session_start();
// include("config.php");
include("functions.php");
// $sql="SELECT `course_num`, `professors`.`name` AS pName, `courses`.`name`AS cName , max_students FROM `courses`, `student_courses`,`professors` WHERE `professors`.`id`=`courses`.`professor_teaching` AND `student_id`= '".$_SESSION['student_id']."'";
// $result = mysqli_query($link, $sql);
$result = viewAllCourses();
?>
<form method="post" action="functions.php">
<table>
<tr>
<th><label>Course No.</label></th>
<th><label>Course Name</label></th>
<th><label>Professor</label></th>
<th><label>Max. Students</label></th>
<th><label>Action</label></th>
</tr>
<?php
if(mysqli_num_rows($result)){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><label name="course_num"><?php echo $row['course_num'];?>
<input type="hidden" name ="coursenumber" value=<?php $row['course_num']?>>
</label></td>
<td><label><?php echo $row['cName'];?></label>
</td>
<td><label><?php echo $row['pName']; ?></label></td>
<td><label><?php echo $row['max_students']; ?></label></td>
<td><input type="submit" name="add" value="add"></td>
</tr>
</form>
</table>
<?php
}
}
?>
然后在functions.php
我有这个代码:
if (isset($_GET['add'])) {
$link = conn();
echo "TST";
exit;
$courseNum= $_POST['coursenumber'];
$record = mysqli_query($link, "INSERT INTO `student_courses`
(`student_id`, `course_id_num`)
VALUES ('".$_SESSION['student_id']."', '$courseNum')");
}
但它什么也没做。
我尝试为 course_number 添加一个输入标签并从那里传递它。但它不起作用。这样做的正确方法是什么?
解决方案
您在所有行中的输入名称都相同。当您提交表单时,$_POST['coursenumber']
将只是表格中的最后一个课程编号,而不是用户单击的那个。您可以将课程编号放在add
按钮的值中,而不是隐藏输入。当一个表单有多个提交按钮时,值来自被点击的那个。
您需要修复</form>
and</table>
标记的顺序,以便它们正确嵌套。
<?php
if(mysqli_num_rows($result)){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><label name="course_num"><?php echo $row['course_num'];?>
</label></td>
<td><label><?php echo $row['cName'];?></label>
</td>
<td><label><?php echo $row['pName']; ?></label></td>
<td><label><?php echo $row['max_students']; ?></label></td>
<td><input type="submit" name="add" value="<?php echo $row['course_num'];?>"></td>
</tr>
</table>
</form>
<?php
}
}
?>
此外,由于您使用 提交表单method="POST"
,因此按钮将是$_POST['add']
,而不是$_GET['add']
。
您应该使用准备好的语句来防止 SQL 注入。
if (isset($_POST['add'])) {
$link = conn();
$courseNum= $_POST['add'];
$stmt = mysqli_prepare("INSERT INTO student_courses (student_id, course_id_num) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "ii", $_SESSION['student_id'], $courseNum);
mysqli_stmt_execute($stmt);
}
如果要传递多个字段,可以将具有多个隐藏输入的单独表单放入每一行,而不是使整个表格成为一个表单。
<?php
if(mysqli_num_rows($result)){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><label name="course_num"><?php echo $row['course_num'];?>
</label></td>
<td><label><?php echo $row['cName'];?></label>
</td>
<td><label><?php echo $row['pName']; ?></label></td>
<td><label><?php echo $row['max_students']; ?></label></td>
<td><form action="functions.php" method="post">
<input type="hidden" name="coursenumber" value="<?php echo $row['course_num'];?>">
<niput type="hidden" name="something" value="<?php echo $row['something'];?>">
<input type="submit" name="add" value="add">
</form></td>
</tr>
</table>
<?php
}
}
?>
然后functions.php
脚本可以使用$_POST['course_num']
and$_POST['something']
来获取这些参数。
推荐阅读
- php - OpenConnect with Apigility 用 JWT 回答
- c++ - 全局变量中的静态 const 与 const
- html - 在 3x 3 网格系统中对齐引导卡
- javascript - 如何创建必须根据 $ID 更改的 javascript 结果 div
- excel - Excel求解器/opensolver找到城市之间的最短距离
- powershell - 带有设置文件的 AzCopy 的 Powershell 脚本
- python - 无法在 Web 服务器上执行 Python 脚本
- javascript - 反应复选框组/单个复选框
- python - Python 会在脚本末尾自动取消设置环境变量吗?
- python - 在 NumPy 数组中查找搜索项的索引