Jesua
Jesua

Reputation: 11

How to populate a dropdown menu in ahtml form

I'm trying to populate a dropdown field inside a html form, but the field doesn't show any value, only a blank one.

I'm trying the following code:

<select name="Select" class="textfields" id="prods">
    <option id="0">--Producto--</option>

<?php
require("conectdb.php");
$allproducts = mysql_query("SELECT * FROM Productos");
while ($viewallproducts = mysql_fetch_array($allproducts)){
?>
    <option id="<?php echo $viewallproducts["ID"];?>"><?php echo $viewallproducts["CODIGO"];?></option>

<?php } ?>

</select>

I have changed the quotes and ;... But still nothing, here is the code of connection to the Database (conectdb.php):

 <?php
$con=mysqli_connect("localhost","xxxxxx","xxxxxx","xxxxxx");

// Check connection
if (mysqli_connect_errno())
  echo "Failed to connect to MySQL: " . mysqli_connect_errno();

mysqli_close($con);
?> 

It seems to be a problem of the database connection, Im trying now the following code to see what happens:

    <?php
 $username = "xxxxx";
$password = "xxxxx";
$hostname = "xxxx"; 

$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


    $allproducts = mysql_query("SELECT * FROM PRODUCTOS");
    while ($viewallproducts = mysql_fetch_array($allproducts)){
        echo ($viewallproducts);
    }
        ?>

But, on this php, I receive the following error:

Warning: mysql:fetch_array() expects parameter 1 to be resource.

I have also tried with mysql_fetch_row() and gives me the same error.

Upvotes: 1

Views: 107

Answers (4)

user2467577
user2467577

Reputation:

Some edit on your code:

<select name="Select" class="textfields" id="prods">
<option id="0">--Producto--</option>

<?
require("conectdb.php");
$allproducts = mysql_query("SELECT * FROM Productos")
or die(mysql_error());
while ($viewallproducts = mysql_fetch_assoc($allproducts)){
?>
    <option id="<?=$viewallproducts['ID'];?>" value="<?=$viewallproducts['ID'];?>">
    <?=$viewallproducts['DESCRIPTION'];?></option>

<?php } ?>

</select>

You should use mysql_fetch_assoc instead of mysql_fetch_array. You should also check if the sql query succeed first.

Upvotes: 0

user2993479
user2993479

Reputation: 601

You forget to put the semicolon after the line

require(conectdb.php)

And you should add a quotation

require("conectdb.php");

Maybe is that

Upvotes: 0

Robert
Robert

Reputation: 20286

I think you made a mistake because you think ID in html is ID in database. You need to use "value" attribute for option

chance this line

<option id="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>

to

<option value="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>

You have also forget about:

  1. require argument in ''
  2. ; after require
  3. Function to get rows should be 'mysql_fetch_row' instead of 'mysql_fetch_array'

I would also suggest you using better DB engine for example PDO or MySQLi because MySQL_* functions are depracated.

Code formatted:

<select name="Select" class="textfields" id="prods">
<option value="0">--Producto--</option>
<?php
    require('conectdb.php'); // be sure that file exist you forgot about '' and ;
    $allproducts = mysql_query("SELECT * FROM `Productos`");
    while ($viewallproducts = mysql_fetch_row($allproducts))
     echo '<option value="'.$viewallproducts['ID'].'">'.$viewallproducts['DESCRIPCION'].'</option>';
?>
</select>

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

1) You have error in require line(put file name inside quotation )
2) you don't put value attribute in option.

Try this:

 <select name="Select" class="textfields" id="prods">
        <option id="0" value="o">--Producto--</option>

        <?php
        require("conectdb.php");
    $allproducts = mysql_query("SELECT * FROM Productos");
    while ($viewallproducts = mysql_fetch_array($allproducts)){
        ?>
        <option id="<?php echo $viewallproducts['ID'];?>" value="<?php echo $viewallproducts['ID'];?>"><?php echo $viewallproducts['DESCRIPCION'];?></option>

    <?php } ?>

    </select>

Upvotes: 1

Related Questions