我有这段代码:
<html>
<body>
<form method="post" action="">
Category: <select name="category">
<option>Choose Category</option>
<?php
include("connect.php");
$select="SELECT * FROM category";
$result=mysqli_query($link,$select) or die (mysqli_error($link));
while($row=mysqli_fetch_array($result))
{
echo "<option value='$row[category]'>".$row['category'];
}
?>
</select>
<br>
Subcategory:<select name="category">
<?php
include("connect.php");
$category=@$_POST['category'];
if($category=="Friuts")
{
$select="SELECT * FROM subcategory WHERE $category='$category'";
$result=mysqli_query($link,$select) or die (mysqli_error($link));
while($row=mysqli_fetch_array($result))
{
echo "<option value='echo $row[subcategory]'>".$row['subcategory'];
}
}
?>
</select>
<br>
<input type="submit" value="Open" name="submit">
</form>
</body>
</html>
我有两个表,表一个名称类别和表两个名称子类别,我想要两个当我从表类别中选择一个项目选项时,从选择选项中的表子类别获取数据出现在下面的图像中:
答案 0 :(得分:1)
要帮助你实现你想要的目标:
if( isset( $_POST['category'] ) ) {}
。如果是,请在子类别表上运行查询并检查cat_id = $ _POST [&#39; category&#39;] 或简而言之:
echo "<option value='$row[cat_id]'>".$row['category'];
$select='SELECT * FROM subcategory WHERE cat_id="' . mysqli_real_escape_string($link, $category ).'"';
未经过测试 并请阅读网站&#39; Alex Howansky&#39;提供!