overboard182
overboard182

Reputation: 300

echo statement with php array

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>

enter image description here

Upvotes: 0

Views: 74

Answers (1)

Logan Wayne
Logan Wayne

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:

  • Put the <select></select> outside your while loop.
  • Make sure also that you are connecting to your database properly.
  • Concat properly
  • Do you receive any error when you run the code you have provided?
  • Your table name is really all in upper case? 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

Related Questions