我正在构建一个小型库存系统,并希望有一个下拉菜单供用户选择一个零件,然后输入零件的数量。我将使用mySQL数据库中的元素填充下拉菜单。我熟悉mySQL,而不是php和html,但是我用java编程,所以我有一些编码经验。无论如何,这是代码,我目前的问题是其他项目没有出现在下拉菜单中。
关于代码的任何其他建议一般都很棒,谢谢!
代码:
<html>
<body>
<form action="index.php" method="post">
<select>
<!-- php code to get all items -->
<?php
$conn = mysqli_connect("localhost", "root", "root", "inventory");
if(mysqli_connect_errno($conn)) {
echo "ERROR: " . mysqli_connect_error();
}
$sql = "SELECT * FROM item_names";
echo $sql;
while($row = mysql_fetch_array($sql)) {
echo '<option value="$row[name]"> $row[name] </option>';
}
?>
<option value="laptop"> Laptop </option>
<option value="ipad"> iPad </option>
</select>
Amount: <input type="text" name="amount" size="5" />
<input type="submit" value="Enter" />
</form>
<form action="new.php" method="post">
Name: <input type="text" name="name" />
<input type="submit" value="New..." />
</form>
<br><br><br><br><br>
<h3> Inventory: </h3>
<table border="1">
<!-- php code to fill table -->
<tr>
<td>(0, 0)</td>
<tr>
<tr>
<td>(1, 0)</td>
<tr>
</table>
</body>
答案 0 :(得分:1)
mysql
和mysqli
个功能。echo $sql
。$row[name]
)的数组键是一种不好的做法。答案 1 :(得分:0)
如果你试试这个呢?
echo '<option value="'.$row['name'].'">' .$row['name']. '</option>';
答案 2 :(得分:0)
<?php
$conn = mysqli_connect("localhost", "root", "root", "inventory");
if(mysqli_connect_errno($conn)) {
echo "ERROR: " . mysqli_connect_error();
}
$sql = "SELECT * FROM item_names";
$result=mysqli_query($sql); // not executing this
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$name=$row["name"];
echo '<option value="'.$name.'">'. $name .'</option>';
}
?>
实际上你没有在sql上运行mysqli_query
并尝试不混淆mysql
和mysqli
函数
答案 3 :(得分:-1)
试试这个
<form action="index.php" method="post">
<select>
<!-- php code to get all items -->
<?php
$conn = mysqli_connect("localhost", "root", "root", "inventory");
if(mysqli_connect_errno($conn)) {
echo "ERROR: " . mysqli_connect_error();
}
$sql = "SELECT * FROM item_names";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<option value="$row[name]"> $row[name] </option>';
}
?>
<option value="laptop"> Laptop </option>
<option value="ipad"> iPad </option>
</select>
Amount: <input type="text" name="amount" size="5" />
<input type="submit" value="Enter" />
</form>
<form action="new.php" method="post">
Name: <input type="text" name="name" />
<input type="submit" value="New..." />
</form>
<br><br><br><br><br>
<h3> Inventory: </h3>
<table border="1">
<!-- php code to fill table -->
<tr>
<td>(0, 0)</td>
<tr>
<tr>
<td>(1, 0)</td>
<tr>
</table>
</body>