您好我正在尝试为我的一位朋友构建一个网络应用程序来帮助她开展业务。该应用程序旨在帮助她监控她的小面包店存储库存单位的数量。(或监控她的库存中剩余多少面粉,一盘鸡蛋,一些黄油)。所以为了做到这一点,我为她做了一个表格。看:
<tr> <form method="POST" action="makeprod.php">
<td>Prodname</td>
<td> <input type="text" name="prodname"></td>
</tr>
<tr>
<td>Quantity</td>
<td> <input type="int" name="quantity"></td>
</tr>
<tr>
<td>Recipe 1: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."'>".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec1qty"></td>
</tr>
<tr>
<td>Recipe 2: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec2qty"></td>
</tr>
<tr>
<td>Recipe 3: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."'>".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec3qty"></td>
</tr>
<tr>
<td>Recipe 4: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe4'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec4qty"></td>
</tr>
<tr>
<td>Recipe 5: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe5'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>"; ['itemname']."</option>";
}
echo "</select>";
?>
</td>
<td>Qty</td>
<td> <input type="int" name="rec5qty"></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value="submit"></td>
</tr>
</form>
</table>
我在这种形式上想要做的是提示她记录她正在制作的产品/糕点,数量并指出将花费她制作该产品的配方/库存,以便减少这些食谱从数据库表中我命名为“库存”(但这是另一个故事)。
我的问题是,我无法确定制作单个产品需要多少成分,重复5或10次配方的选择选项似乎使我的代码看起来太脏而且看起来不像高效。我想要的是一个代码,它将帮助我只显示表单中所需的选项数量。因此,如果她制作的产品只有6种不同的成分,那么只能显示6种选择。如果是3,那么只有3。 如果我不必重复上面的代码来使我的代码至少更清洁,那将会有所帮助:
<td>Recipe 1: </td>
<td>
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
$sql = "SELECT itemname FROM inventory";
$result = mysql_query($sql);
echo "<select name='recipe1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value = ' ". $row ['itemname'] ."' >".$row['itemname']."</option>";
}
echo "</select>";
?>
我是PHP的新手,并不知道Javascript,所以我更喜欢HTML / PHP格式的解决方案,但JS也足够了。谢谢。
答案 0 :(得分:0)
根据您的问题标题,您在代码中重复了sql连接代码。通过添加单个连接文件来避免它。请创建connection.php并在其中添加以下代码。
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('inventory system');
?>
并将此文件包含在此工作页面中
include "connection.php";