【发布时间】:2013-07-21 13:56:39
【问题描述】:
PHP 新手在这里。目标是:
a) 从数据库记录中动态显示预订选项(健身课程)表,允许用户选择多个选项,每行带有复选框 - 这部分有效。
b) 将复选框选择传递给在确认页面上列出所选数据的表格。我在这里遇到一个错误:为 foreach() 提供的参数无效。
c) 当用户点击第二页的“确认”按钮时更新数据库。
迄今为止的研究发现 this advice 使用 $_GET 和 $_POST 通过数组实现这一目标。
我在初始页面上的复选框代码:
echo '<form action="makebooking.php" method="get">';
echo '<td><input type="checkbox" name="class_id[]" value=' . $row['class_id'] . '</td></tr>';
foreach 语句错误来自在第二页生成选择表的这段代码:
//Check if the GET is set from classes.php
if (isset($_GET['class_id'])) {
// Grab the score data from the GET
foreach($_GET['class_id'] as $class_id) {
$_GET['class_id'] = $class_id;
}
}
//table header
echo '<table class="table table-bordered table-hover">';
echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th> <th>Add someone</th></tr></thead>';
//create the form
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
//Get the class IDs from the GET to use in the POST
foreach ($_GET['class_id'] as $class_id) {
$sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
$data = mysqli_query($dbc, $sql);
//---------------------------------------------------------------------------------
//get table data
while ($row = mysqli_fetch_array($data)) {
$date = $row["new_date"];
$time = $row["new_time"];
$venue = $row["venue"];
$class_id = $row["class_id"];
}
//---------------------------------------------------------------------------------
//Show a table of the selected classes
echo '<input type="hidden" name="id" value= ' . $class_id . ' />';
echo '<td>' . $date . '</td>';
echo '<td>' . $time . '</td>';
echo '<td>' . $venue . '</td>';
echo '<td>' . $username . '</td>';
echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
}
echo'</table>';
// Make booking button
echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
echo '</form>';
}
两个页面的完整代码this pastebin。感谢所有错误修复建议!
【问题讨论】:
-
通过 POST 或 GET 打开页面是否有错误?
-
$_GET['class_id']这个变量的设置形式在哪里? -
您的表单似乎正在提交
action="post",这意味着您要查找的变量应该是$_POST['class_id'] -
编辑代码以显示更多内容:第一个页面表单使用 GET,第二个页面设置 $class_id 变量,应该列出选择,但发生错误在检索 GET 数据时。
标签: php mysql post checkbox get