Updating HTML combo box from MySQL database using PHP

need help here. This is a very simple web application that I am developing, but just need help with something.

Here is the setting. I have an html form with one combo box. All I need to do is update this combo box from the mysql table named provider. The entry in this β€œprovider” table is through a different form on my website that I have already installed. I need help updating this combo box from the provider table automatically. Please let me know the php code. I have included my code too. Thank you in advance! I also added an html form.

Here's what's happening after making the edits you guys suggested :)

+4
source share
4 answers

replace your code

$result = mysql_query("SELECT supplier FROM supplier"); while($row = mysql_fetch_array($result)) { /*echo '<form action="">';*/ echo "<select name='supplier'>"; echo "<option value = '$row[supplier]'>""</option>"; echo "</select>"; 

with

  $result = mysql_query("SELECT supplier FROM supplier"); echo "<select name='supplier'>"; while($row = mysql_fetch_assoc($result)) { echo "<option value = '".$row[supplier]."'>".$row[supplier]."</option>"; } echo "</select>"; 
+3
source

So,

What is the problem? Does your script work or do you want to update the active Combobox on one screen if a new entry for the provider is entered on the other screen?

Ok, some tips:

  • "select" -Tag should be placed outside the loop.
  • Put the column name in "
  • Add display value for parameters

So without checking:

 /*echo '<form action="">';*/ echo "<select name='supplier'>"; while($row = mysql_fetch_array($result)) { echo "<option value = '".$row["supplier"]."'>".$row["supplier"]."</option>"; } echo "</select>"; 
0
source

Your problem is here:

  $result = mysql_query("SELECT supplier FROM supplier"); while($row = mysql_fetch_array($result)) { /*echo '<form action="">';*/ echo "<select name='supplier'>"; echo "<option value = '$row[supplier]'>""</option>"; echo "</select>"; 

A Select list is created inside the mysql data loop. As @Hitesh explained. You need to create this outside the loop and only discard the results of the data inside. For instance:

 $result = mysql_query("SELECT supplier FROM supplier"); echo "<select name='supplier'>"; while($row = mysql_fetch_array($result)) { echo "<option value=".$row['supplier'].">".$row['supplier']."</option>"; } echo "</select>"; 

This will exit your drop-down list with all your provider names as the value and the displayed text options.

If you tried to do the following:

  $result = mysql_query("SELECT supplier FROM supplier"); while($row = mysql_fetch_array($result)) { /*echo '<form action="">';*/ echo "<select name='supplier'>"; echo "<option value = '$row[supplier]'>""</option>"; echo "</select>"; } 

As a result, you will have as many drop-down boxes as you had suppliers in your database. This is because you create a new Select for each record found.

In addition, without specifying a second $row['supplier'] between the option tags, you simply get an empty (empty) drop-down box.

Hope this helps.

0
source

Here is the complete code:

PHP code:

 <?php // select box open tag $selectBoxOpen = "<select name='supplier'>"; // select box close tag $selectBoxClose = "</select>"; // select box option tag $selectBoxOption = ''; // connect mysql server $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } // select database mysql_select_db("rtgs", $con); // fire mysql query $result = mysql_query("SELECT supplier FROM supplier"); // play with return result array while($row = mysql_fetch_array($result)){ $selectBoxOption .="<option value = '".$row['supplier']."'>".$row['supplier']."</option>"; } // create select box tag with mysql result $selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose; ?> 

HTML code:

 <form action="ntxn.php" method="post"> <table> <tr> <td>Supplier Name:</td> <td> <?php echo $selectBox;?> </td> </tr> <tr> <td>Bill No. :</td> <td><input type ="text" name="billno"/></td> </tr> <tr> <td>Bill Date : </td> <td><input type="date" name="billdate"/></td> </tr> <tr> <td>Bill Amount : </td> <td><input type="text" name="billamt"/></td> </tr> <tr> <td>NEFT / RTGS No. :</td> <td><input type="text" name="rtgs"/></td> </tr> <tr> <td><input type="submit" name="Save"/></td> </tr> </table> </form> 
0
source

Source: https://habr.com/ru/post/1491068/


All Articles