Reputation: 300
I am only just getting into PHP coding. I know what I may have isn't considered best practice but my question is
how can I create the select portion of the form in an echo statement.
I am trying to select rows from a database with mysqli_fetch_array.
I want to go through each row of my database and create the select portion of the form so that each selection will have a value corresponding to the vendor_id and I want the selection to print out the names of the vendor for the user to select.
At the end, I want to have something like the picture I posted, without having to hardcode the vendor information.
<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>
Upvotes: 0
Views: 74
Reputation: 5991
I think the question is simple. OP just wants to populate the <select></select>
field with vendors coming from the database. And when a new vendor is added, and a user visited the form, the vendor field will be updated with newly added vendor/s.
Just wanted to point out:
<select></select>
outside your while loop.VENDOR
? Remember that MySQL is 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>
Upvotes: 2