0

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

3
  • 1
    you want the form populated on the fly on change of the select box? you might need to use an xmlhttprequest for that. Commented Sep 23, 2016 at 1:15
  • when the page is created I want to be able to refer to the database, check my vendors, and make those vendors selectable options. If the database changes, I want the page to be dynamic in the sense that next time I open it, it will include new vendors as options. Commented Sep 23, 2016 at 1:18
  • 1
    You don't have to echo all the HTML. You can just write it plain and put any PHP in PHP tags i.e. <?php ... ?> Commented Sep 23, 2016 at 1:56

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

very clear answer. Actually came up with the solution last night but forgot to edit. My issue was concat and the select tags. Currently connecting to a database that is not my own, so I do not have privileges to edit names and such. table names are all caps, yes. In any event, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.