用php构建表单
我正在建立一个小型库存系统,并希望有一个下拉菜单,供用户选择一个部分,然后输入该部分的金额。我将用mySQL数据库中的元素填充下拉菜单。我熟悉mySQL,不太熟悉php和html,但我用java编程,所以我有一些编码经验。不管怎样,这是代码,我目前的问题是其他项目只是没有出现在下拉菜单中用php构建表单,php,html,mysql,Php,Html,Mysql,我正在建立一个小型库存系统,并希望有一个下拉菜单,供用户选择一个部分,然后输入该部分的金额。我将用mySQL数据库中的元素填充下拉菜单。我熟悉mySQL,不太熟悉php和html,但我用java编程,所以我有一些编码经验。不管怎样,这是代码,我目前的问题是其他项目只是没有出现在下拉菜单中 <form action="index.php" method="post"> <select> <!-- php code to
<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>
关于代码的任何其他建议都将非常棒,谢谢
<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>
代码:
<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>
<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>
笔记本电脑
iPad
数量:
姓名:
库存:
(0, 0)
(1, 0)
如果你试试这个,也许
echo '<option value="'.$row['name'].'">' .$row['name']. '</option>';
<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>
echo'.$row['name'].';
试试这个
<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>
笔记本电脑
iPad
数量:
姓名:
库存:
(0, 0)
(1, 0)
mysql
和mysqli
函数回显$sql
$row[name]
)是一种不好的做法
实际上,您没有在sql上运行
mysqli\u查询
,并且尽量不要混淆mysql
和mysqli
函数mysql
,mysqli
-您使用的是什么库,嗯?删除echo$sql
。下面有一个正确的答案,在while中回音。混合使用mysqli_u和mysql_u可能是不明智的。我还建议将html和php分开,因为本网站上的许多问题都与代码混乱导致的编码错误有关。
<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>