这是代码。它完美地显示了我想要的所有元素。但是当我点击“注册”时,它只为任何选定的元素注册一个元素。可能的问题是什么?
以下是代码:
if(array_key_exists("id", $_SESSION)) {
include("connection.php");
$query = "SELECT * from `classes`";
echo '<div id="accordion" role="tablist" aria-multiselectable="true"> ';
$result = mysqli_query($link, $query);
$results = array();
$rows = array();
$queries = array();
while($row = mysqli_fetch_array($result)) {
array_push($rows, $row);
}
for ($i = 0; $i < count($rows); $i ++) {
$name = $rows[$i]["name"];
$start_time = $rows[$i]["begTime"];
$end_time = $rows[$i]["endTime"];
$price = $rows[$i]["price"];
$places = $rows[$i]["placesAvailable"];
if (array_key_exists("enroll", $_POST)) {
$queries[$i] = "UPDATE `users` SET sectionID = ".$rows[$i]["ID"]." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";
$results[$i] = mysqli_query($link, $queries[$i]);
}
echo '
<div class="card">
<div id = "section" class="card-header" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
'.$name.'
</a>
</h5>
</div>
<div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
'.$name.'
<p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p>
<p> <strong> Price: </strong>'.$price.' </p>
<p> <strong> Places available: </strong>'.$places.' </p>
<form action="" method = "post">
<input class="btn btn-success" type="submit" name = "enroll" value = "Enroll">
</form>
</div>
</div>
</div>
';
}
echo '</div>';
}
答案 0 :(得分:0)
您的逻辑不太正确,因为您没有将所需的课程ID与提交的enroll
按钮一起发送到脚本。
我已经调整了你的代码。
只有当您的用户/学生当时只能注册一门课程时,此逻辑才有效。
if(array_key_exists("id", $_SESSION)) {
include("connection.php");
if (array_key_exists("enroll", $_POST)) {
$query = "UPDATE `users` SET sectionID = ".mysqli_real_escape_string($link, $_POST["ID"])." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";
mysqli_query($link, $query);
}
$query = "SELECT * from `classes`";
echo '<div id="accordion" role="tablist" aria-multiselectable="true"> ';
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
$name = $row["name"];
$start_time = $rows["begTime"];
$end_time = $rows["endTime"];
$price = $rows["price"];
$places = $rows["placesAvailable"];
echo '
<div class="card">
<div id = "section" class="card-header" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
'.$name.'
</a>
</h5>
</div>
<div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
'.$name.'
<p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p>
<p> <strong> Price: </strong>'.$price.' </p>
<p> <strong> Places available: </strong>'.$places.' </p>
<form action="" method = "post">
<input type="hidden" name="ID" value="'.$row["ID"].'" />
<input class="btn btn-success" type="submit" name = "enroll" value = "Enroll">
</form>
</div>
</div>
</div>
';
}
echo '</div>';
}
<强>更新强>
要让用户选择多个课程,您只需要一个带有复选框数组的表单。您的$_POST["ID"]
将是一个类ID的数组。在下面的示例中,它被序列化存储在您的数据库中,假设sectionID
是字符串。在将数组保存到数据库之前,您还可以json_encode
数组。如果您希望sectionID
为数字,则需要创建一个新表来存储每个用户的所有类。
if(array_key_exists("id", $_SESSION)) {
include("connection.php");
if (array_key_exists("enroll", $_POST)) {
$query = "UPDATE `users` SET sectionID = ".mysqli_real_escape_string($link, serialize($_POST["ID"]))." WHERE id = ".mysqli_real_escape_string($link, $_SESSION['id'])." LIMIT 1";
mysqli_query($link, $query);
}
$query = "SELECT * from `classes`";
echo ' <form action="" method = "post">
<div id="accordion" role="tablist" aria-multiselectable="true"> ';
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
$name = $row["name"];
$start_time = $rows["begTime"];
$end_time = $rows["endTime"];
$price = $rows["price"];
$places = $rows["placesAvailable"];
echo '
<div class="card">
<div id = "section" class="card-header" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
'.$name.'
</a>
</h5>
</div>
<div id="collapse" class="collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
'.$name.'
<p> <strong> Time: </strong>'.$start_time.' - '.$end_time.' </p>
<p> <strong> Price: </strong>'.$price.' </p>
<p> <strong> Places available: </strong>'.$places.' </p>
<input type="checkbox" name="ID[]" value="'.$row["ID"].'" />Enroll
</div>
</div>
</div>
';
}
echo '</div>
<input class="btn btn-success" type="submit" name = "enroll" value = "Submit">
</form>';
}