我有一个带有ID,名称和其他属性的学生表。我也有另一个带有student_id,class_id和其他属性的表格student_info。我在网址中传递了学生ID,例如:
....admin/student_info.php?id=5
我知道它可以工作,因为我可以从学生表中提取信息并显示它。我无法不断更新此错误,因此无法更新学生表中的student_id。
错误:无法执行INSERT INTO student_info(student_id,class_id)值(?,?)。列“ student_id”不能为空
这是我的代码:
$id = $_GET['id'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo htmlspecialchars($_GET["id"]);
//Check if body is empty
if (empty(trim($_POST["class_id"]))) {
$err = "Please select Class";
} else {
$class_id = trim($_POST["class_id"]);
}
if (empty($err)) {
$sql = "INSERT INTO student_info (student_id,class_id) values (?,?)";
if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ii", $param_stu_id, $param_class_id);
// Set parameters
$param_stu_id = $id;
$param_class_id = $class_id;
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
// Redirect to login page
header('location:view_student.php');
} else {
$err = "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close statement
mysqli_stmt_close($stmt);
}
}
}
在绑定变量之前设置参数:
// Set parameters
$param_stu_id = $id;
$param_class_id = $class_id;
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ii", $param_stu_id, $param_class_id);