我有以下PHP代码,它通过PDO从SQL获取数据:
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$tableName = 'categories';
ob_start();
session_start();
//Get values from table
$sqlprimaryCategory = $dbh->prepare("SELECT * FROM $tableName GROUP BY primary_category");
$sqlprimaryCategory->execute();
echo '<form id="form1" action="" method="post">';
echo '<select name="primary_category" size = "4" onChange="document.getElementById(\'form1\').submit();"> ';
while ($resultprimary = $sqlprimaryCategory->fetch()) {
echo '<option value="';
echo $resultprimary['primary_category'];
echo '">';
echo $resultprimary['primary_category'];
echo '</option>';
}
echo '</select>';
//echo '<input type="submit" name="primary_category_button" id="primary_category_button" value="Submit">';
echo '</form>';
if (isset($_POST['primary_category'])) {
$_SESSION['primary_category'] = $_POST['primary_category'];
//unset($_SESSION['secondary_category']);
//unset($_SESSION['tertiary_category']);
}
//Get values from table
$sqlSecondaryCategory = $dbh->prepare("SELECT * FROM $tableName WHERE primary_category = :primary_category GROUP BY secondary_category");
$sqlSecondaryCategory->execute(array(':primary_category'=>$_SESSION['primary_category']));
echo '<form id="form2" action="" method="post">';
echo '<select name="secondary_category" size = "4" onChange="document.getElementById(\'form2\').submit();"> ';
while ($resultSecondary = $sqlSecondaryCategory->fetch()) {
echo '<option value="';
echo $resultSecondary['secondary_category'];
echo '">';
echo $resultSecondary['secondary_category'];
echo '</option>';
}
echo '</select>';
//echo '<input type="submit" name="secondary_category_button" id="secondary_category_button" value="Submit">';
echo '</form>';
if (isset($_POST['secondary_category'])) {
$_SESSION['secondary_category'] = $_POST['secondary_category'];
//unset($_SESSION['tertiary_category']);
}
//Get values from table
$sqlTertiaryCategory = $dbh->prepare("SELECT * FROM $tableName WHERE secondary_category = :secondary_category GROUP BY tertiary_category");
$sqlTertiaryCategory->execute(array(':secondary_category'=>$_SESSION['secondary_category']));
echo '<form id="form3" action="" method="post">';
echo '<select name="tertiary_category" size = "4" onChange="document.getElementById(\'form3\').submit();"> ';
while ($resultSecondary = $sqlTertiaryCategory->fetch()) {
echo '<option value="';
echo $resultSecondary['tertiary_category'];
echo '">';
echo $resultSecondary['tertiary_category'];
echo '</option>';
}
echo '</select>';
//echo '<input type="submit" name="tertiary_category_button" id="tertiary_category_button" value="Submit">';
echo '</form>';
if (isset($_POST['tertiary_category'])) {
$_SESSION['tertiary_category'] = $_POST['tertiary_category'];
unset($_SESSION['tertiary_category']);
}
if (isset($_POST['primary_category']) OR isset($_POST['secondary_category']) OR isset($_POST['tertiary_category'])) {
echo $_SESSION['primary_category'];
echo $_SESSION['secondary_category'];
echo $_SESSION['tertiary_category'];
}
以上是一个下拉菜单。第二个html下拉列表根据第一个选择的选择显示内容,而thrid下拉列表根据第二个下拉列表的选择显示内容。如何修复我的代码以删除错误?
答案 0 :(得分:2)
如果未发布第二个类别,则不在session
中,因此您将无法访问它。
您可以在第三个请求声明中使用它之前检查$_SESSION['secondary_category']
是否已设置
if (isset($_POST['secondary_category'])) {
$_SESSION['secondary_category'] = $_POST['secondary_category'];
unset($_SESSION['tertiary_category']);
}elseif (!isset($_SESSION['secondary_category'])){
$_SESSION['secondary_category'] = null;
}
答案 1 :(得分:0)
if (isset($_POST['tertiary_category'])) {
$_SESSION['tertiary_category'] = $_POST['tertiary_category'];
//unset($_SESSION['tertiary_category']); < why unset right after assigning? (COMMENT OUT)
}
//Change OR's to AND's because the echo's will only work if all of them are set.
if (isset($_POST['primary_category']) AND isset($_POST['secondary_category']) AND isset($_POST['tertiary_category'])) {
echo $_SESSION['primary_category'];
echo $_SESSION['secondary_category'];
echo $_SESSION['tertiary_category'];
}
// If you want to print out the ones that exist, make separate if statements for each category to see if they exist, or give them a blank value instead of unsetting them.