0

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>
4
Contributors
4
Replies
32
Views
3 Years
Discussion Span
Last Post by Kyle Wiering
0

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

0

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!

0

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;
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.