Hi, I am new to php and html, so I need some help with a problem: I am trying to make a form where you can select from a dropbox, but thae data from the dropbox should be retrieved from a sql database.
If I save the code bellow as php, the dropbox is created and retrieves the data from sql, but then the submit button (together with everything else I put after the while loop) disappeares. If I save it as a html file, the form and submit button works, but it doesn't retrieve any data from sql database.

Thanks

<form action="Techniques.php" method="post">
        <p>Which Disease?
        <select name="disease" >
            <option value="" >--Select--</option>
        <?php
          require_once("dbconnect.php");


      $sql = "SELECT * FROM diseases";
      $result = mysql_query($sql)or die (mysql_error());
            while($row = mysql_fetch_array($result) or die (mysql_error()))
            {
    ?>
             <option value="disease" ><?php echo($row['Disease']);?></option>
    <?php
            }
    ?>        

    </select>
    </p>
    input type="submit" value="Copmlete"/>
</form>

Dani AI

Generated

A subtle issue here is the use of or die(mysql_error()) inside the while condition. Because of PHP operator precedence, $row = mysql_fetch_array($result) or die(...) is evaluated as ($row = mysql_fetch_array($result)) or die(...). When the fetch returns false at the end of the result set, die() runs and the rest of your HTML (including the submit button) never renders. Handle errors on the query itself, not on each fetch. Also, use the record ID for the option value and HTML-escape output. The legacy mysql_* API has been removed in modern PHP; switch to mysqli or PDO.

Example with PDO:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$stmt = $pdo->query('SELECT id, Disease FROM diseases ORDER BY Disease');
$options = '<option value="">--Select--</option>';
foreach ($stmt as $row) {
    $options .= sprintf(
        '<option value="%s">%s</option>',
        htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'),
        htmlspecialchars($row['Disease'], ENT_QUOTES, 'UTF-8')
    );
}
?>
<form action="Techniques.php" method="post">
  <label>Which Disease?
    <select name="disease"><?php echo $options; ?></select>
  </label>
  <input type="submit" value="Complete">
</form>

Be sure to run this through a PHP-enabled web server and save as .php. See PHP operator precedence in the manual at https://www.php.net/manual/en/language.operators.precedence.php and PDO usage at https://www.php.net/manual/en/book.pdo.php.

Recommended Answers

All 5 Replies

You're missing an opening < on your submit button input.

Hi,
As per "pixelsoul" please check syntex i.e. <

Also I have suggest another coding sytle. i.e.
a) Do Not open connection in <select>
b) First Opne Connection and get result
c) Fetch only result loop in <select>
d) You can also use following coding style to better understand and avoid syntex error.

<?php
//Open Connection
          require_once("dbconnect.php");

//Gather Information
      $sql = "SELECT * FROM diseases";
      $result = mysql_query($sql) or die (mysql_error());
?>    
<form action="Techniques.php" method="post">
        <p>Which Disease?
        <select name="disease" >
            <option value="" >--Select--</option>
            <!-- Loop For Fetch Result -->        
            <?php while($row = mysql_fetch_array($result) ) : ?>
             <option value="disease" ><?php echo($row['Disease']);?></option>
            <?php endwhile; ?> 
            <!-- End Loop for Fetch Result -->
    </select>
    </p>
    <input type="submit" value="Copmlete"/>
</form>

------
If you have any more query, Feel free to ask.

Thanks,
Ajay

Hi,
Thanks for the response!
I just deleted '<' by accident when i posted it.
Thanks a lot for your suggestion, I will try it tomorrow at work!

I would also suggest further distinction between PHP and HTML. I also updated your submit value from 'Copmlete'. Something like this:

<?php
//Open Connection
require_once("dbconnect.php");
//Gather Information
$optionRow = '<option value="%s" >%s</option>';

$sql        = "SELECT * FROM diseases";

$result = null;

// There are various ways to handle mysql with exceptions and mysql_error.
try
{
    $result = mysql_query($sql);
}
catch(Exception $e)
{
    error_log('Unable to query MSSQL' . $e->getMessage() . ' - ' . __FILE__ . ' ' . __LINE__);
}

$options    = sprintf($optionRow, '', '--Select--') . PHP_EOL;
// loop though the query results
while($row = mysql_fetch_array($result) )
{
    // setting the value to the disease, otherwise there is no difference regardless of what is selected 
    $options .=  sprintf($optionRow, $row['Disease'], $row['Disease']) . PHP_EOL;
}

// Heredocs are add a layer of separation when looking at the code.
$html = <<<HTML
<div id="diseaseSelection">
    <p>Which Disease?</p>
    <form action="Techniques.php" method="post">
        <select name="disease" >
            {$options}
        </select>
        <input type="submit" value="Complete"/>
    </form>
</div>
HTML;

// send the completed form
echo $html;
?>

Help me fetch column values from MySQL database to HTML option
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.