php数组的echo语句

时间:2016-09-23 01:07:06

标签: php mysql echo

我只是进入PHP编码。我知道我可能拥有的不是最佳实践,但我的问题是

如何在echo语句中创建表单的选择部分。

我正在尝试使用mysqli_fetch_array从数据库中选择行。

我想浏览数据库的每一行并创建表单的选择部分,以便每个选择都有一个与vendor_id相对应的值,我希望选择打印出供用户的供应商名称选择。

最后,我希望得到类似我发布的图片,而不必硬编码供应商信息。

<HTML>
<?php 

# keep the sensitive information in a separated PHP file.
include 'dbinfo.php';

$con=mysqli_connect($server,$user,$pass,$dbname)
     or die("<br>Cannot connect to DB\n");

session_start();
$row = $_SESSION['row'];

$query = " SELECT * FROM VENDOR group by vendor_id";
$result = mysqli_query($con,$query);



echo "<a href=\"CPS5920_employee_login.php\">Employee logout</a>";
echo "<br>";
echo "<font size=4><b>Add products</b></font>";
echo "<br>";


echo "<form name='input' action='CPS5920_product_insert.php' method='post' >
<br> Product Name: <input type='text' name='product_name' required='required'>
<br> description: <input type='text' name='description' required='required'>
<br> Cost: <input type='text' name='cost' required='required'>
<br> Sell Price: <input type='text' name='sell_price' required='required'>
<br> Quantity: <input type='text' name='quantity' required='required'><br>";

while($venrow = mysqli_fetch_array($result, MYSQLI_ASSOC)){
   echo " Select vendor: <SELECT>
   <option value = $venrow['vendor_id']> $venrow['name']</option>
   </SELECT>";
}

#ignore this echo. it is hardcoded at the moment
echo "<br><input type='hidden' name='employee_id' value= '2'>
<br><input type='submit' value='Submit'>
</form>";


mysqi_close($con);

?>
</HTML>

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为问题很简单。 OP只想填充来自数据库的供应商的<select></select>字段。当添加新供应商并且用户访问该表单时,将使用新添加的供应商更新供应商字段。

只想指出:

  • <select></select>放在while循环之外。
  • 确保您正确连接到数据库。
  • Concat正确
  • 运行您提供的代码时是否收到任何错误?
  • 你的桌名真的都是大写的吗? VENDOR?请记住,MySQL是case sensitive
<select name="vendor">

    <?php

        $query = "SELECT * FROM VENDOR group by vendor_id";
        $result = mysqli_query($con, $query);

        while($venrow = mysqli_fetch_array($result)){
            echo '<option value="'.$venrow['vendor_id'].'">'.$venrow['name'].'</option>';
        }

    ?>

</select>