4

need some help here. It's a very simple web app i'm developing but just needed some help with something.

Here's what's setup. I have a html form with one combo box. All I need is to update this combo box with the entries from a mysql table named 'supplier'. The input to this table 'supplier' is via another form on my website which i've already setup. I need help in auto updating this combo box from the table 'supplier'. Please let me know the php code for it. I've included my code as well. Thanks in advance! I have included the html form as well.

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

2
  • did u get anything wrong with this code? place <select> tag before while loop Commented Jul 12, 2013 at 8:01
  • Tip: change your database connect to this: $con = mysql_connect("localhost","user","pass") or die('Could not connect: ' . mysql_error()); mysql_select_db("rtgs", $con) or die('Could not select database'); Then put this code in a seperate file (connect.php) and include this at the top of every page you need to connect to your database from with include('connect.php'); Commented Jul 12, 2013 at 8:01

4 Answers 4

3

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

7 Comments

I never understood why we use the following code : echo "<option value = '".$row[supplier]."'>".$row[supplier]." </option>"; I understood the first part, ie echo "<option value = '".$row[supplier]."'>"</option>. But why do we include the extra .$row[supplier].
@vjrngn, the extra $row[supplier] for text into SELECT box which display to user. If you don't wrote second, then SELECT box appear blank
in place of the combo box on my page, this is what is get : "; while($row = mysql_fetch_assoc($result)) { echo ""; } echo ""; ?>
i have edited my original post and added the output that i got after editing the code. check it out
here is the code you sent me. i just updated it .. but still no luck : <?php $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>"; ?>
|
0

So,

what is the problem? Is your script not working or do you want a active Combobox on one screen to be updated, when on another screen a new entry for supplier is entered?

Ok, some hints:

  1. The "select"-Tag should be placed outside the loop.
  2. Put the column name in "
  3. Add a display-Value for the options

So, without checking it:

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

2 Comments

I just want an active combo box to be updated when on another screen a new entry for supplier is entered. :)
Really? That will be weird AJAX-Stuff!
0

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>"; 

You're creating the drop down box (the Select) inside of the mysql data loop. As @Hitesh has explained. You need to create this outside of the loop and only echo out the data results within. For example:

$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 output your drop down box, with all your supplier names as the value and the displayed text options.

If you attempted 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>"; 
                    }

You would just end up with as many drop down boxes as you had suppliers in your database. This is because you're creating a new Select box for each record found.

Also, without specifying the second $row['supplier'] between the option tags, you'd just end up with a blank (empty) drop down box.

Hope this helps.

1 Comment

Yeah I got that @LokiSinclair :) thanks.. but im still not getting what i want. I'm posting a screenshot of what's happening.
0

Here is 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>

Comments

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.