[英]Error: Undefined Index php
I have created a DB which consists of courses. 我创建了一个包含课程的数据库。 Students logged in can choose the courses using a dynamic checkbox and submitting it via a form.
登录的学生可以使用动态复选框选择课程,然后通过表格提交。
<form method="POST" action="checkbox.php">
<?php
$sql1 = "SELECT course_id FROM course WHERE sem ='" .$_POST['sem']."'";
$result = mysqli_query($conn,$sql1);
while($row = mysqli_fetch_array($result)) {
echo "<input type='checkbox' name='course_id[]' value='{$row['course_id']}'>" . $row['course_id'];
}
?>
<br>
<button ><span>Submit</span></button>
</form>
The form calls the PHP which consists of an array to echo the values. 该表单调用PHP,该PHP由数组组成,以回显值。 But I am getting an error from the php which says "undefined index 12IS36"..Here 12IS36 is the course stored in the DB.
但是我从php中得到一个错误,提示“未定义的索引12IS36”。此处12IS36是存储在数据库中的课程。
PHP Code PHP代码
<?php
//connection
foreach($_POST['course_id'] as $course) {
echo $_POST[$course];
}
?>
You should not use a value like an index .. 您不应该使用索引等值。
<?php
//connection
foreach($_POST['course_id'] as $course) {
echo $_POST[$course]; // in this case you are using a value like an index
}
?>
you should use instead 你应该改用
<?php
//connection
foreach($_POST['course_id'] as $course) {
echo $course;
}
?>
Maybe this will help you, i've improved the code slightly and escaped the clause to stop SQL injections. 也许这会对您有所帮助,我对代码进行了一些改进,并转义了该子句以停止SQL注入。
<form method="POST" action="checkbox.php">
<?php
$sem = mysqli_real_escape_string($conn, $_POST['sem']);
$sql = mysqli_query($conn, "SELECT course_id FROM course WHERE sem='$sem'");
while($row = mysqli_fetch_assoc($sql)) {
echo "<input type='checkbox' name='course_id[]' value='{$row['course_id']}'> ".$row['course_id'];
}
?>
<br>
<button><span>Submit</span></button>
</form>
<?php
//connection
foreach($_POST['course_id'] as $course) {
echo $course;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.