我一直在尝试从下拉菜单中的数据库mysqli中提取数据。这是我的postad.html代码。我想从类别表中提取行名称CatName的类别数据。我也有显示ctegory id的CatId。提前谢谢。请帮忙:
<html>
<head>
<title>Post AD</title>
</head>
<body>
<form method="POST" action="postad.php">
<table>
<tr>
<td>AdName:</td>
<td><input type="text" name="adname"></td>
</tr>
<tr>
<td>AdCategory</td>
<td>
//dropdown list
<select name="Categories">
<?php include = connect.php
$sql = "SELECT CatName FROM categories order by CatName";
$result = $db->query($sql);
while($row = $result->fetch_array()) {
echo "<option value='". $row['CatName']."'>."</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td>Contact Number</td>
<td><input type="text" name="contactnumber"></td>
</tr>
<tr>
<td>Image</td>
<td><input type="text" name="image"></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>Expiration Date</td>
<td><input type="text" name="expirationdate"></td>
</tr>
<tr>
<td id="btn"><input type="submit" value='submit' name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:1)
您的菜单代码似乎有误。应该是这个
echo "<option value=". $row['CatName'] ."></option>';
反对
echo "<option value='". $row['CatName']."'>."</option>';
表中的结果将是一个字符串,因此无需担心value =''。真正的问题是你如何贬低你的报价。
编辑:注意到include = connect.php
应为include 'connect.php';
<?php
$mysql_host = '';
$mysql_user = '';
$mysql_pass = '';
$mysql_dbase = '';
$mysql_table = '';
$pdo = new PDO('mysql:host=' . $mysql_host . ';dbname=' . $mysql_dbase, $mysql_user, $mysql_pass);
$sth = $pdo->prepare('SELECT * FROM `' . $mysql_table . '` ORDER BY `id` CatName');
$sth->execute();
$result = $sth->fetchAll();
foreach ($result as $row) {?>
<option value=<?php echo $row['CatName']; ?>></option>;
<?}?>
答案 1 :(得分:0)
echo "<option value='". $row['CatName']."'>."</option>';
包含错误,并且没有内部html选项
为简单起见,您可以使用这种方式。
$CatName=$row["CatName"];
echo '<option value="'.$CatName.'">'.$CatName.'</option>';
答案 2 :(得分:0)
确定。现在我创建了一个名为getcategories.php的新php文件。这个页面效果很好。这是它的代码:
$sql = "SELECT CatName FROM categories order by CatName";
$result = $db->query($sql);
$options="";
//echo "<select value='categories'>";
while($row = $result->fetch_assoc()) {
$categoryname=$row["CatName"];
// echo '<option value="'.$categoryname.'">'.$categoryname.'</option>';
// echo "</select>";
//echo "$categoryname\n";
$options .= '<option value="'.$categoryname.'">'.$categoryname.'</option>';
}
?>
我应该如何将选项数据提取到html文件的下拉列表