1

I need help regarding my PHP drop down list. I've created a Database call state. I'm trying to populate a state drop down on an html page using php connecting to mysql. Here is my code for the html page:

<form action="CreateUser.php" method="POST">
            <label type='text'>State:</label>
            <select name='state'>
            <option value='0'>--Choose a State--</option>
            <?php   
                    $dbTable='states'
                    $QueryResult=msql_query('Select * from       "$dbTable"'); 

                    while($Row = mysql_fetch_assoc($QueryResult))
                    {
                    ?>
                        <option value="<?php echo $Row['StateID']; ?>">
                            <?php echo {$Row['StateName']}; ?>
                        </option>

            <?php   } ?>

            </select>


            <input id='movebutton' type = "Submit" name="submit" value="submit"/>
        </form>

And here is my PHP code for the dbconnection:

$DBName = "business"; 
$DBConnect = @mysql_connect("localhost", "root", "");

if($DBConnect === FALSE)
{
    echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>"; 
}
else 
{       

    $DB = mysql_select_db($DBName, $DBConnect); 

    if(!$DB)
    {
        echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>";

        mysql_close($DBConnect); 
        $DBConnect = FALSE; 
    }

}

Can someone tell me what I'm doing wrong? I've checked on this forum and also on YouTube regarding PHP dropdown. I'm new to php and still learning.

1
  • are you sure you have put correct credentials and server information of db? Commented Apr 11, 2014 at 15:24

2 Answers 2

1

The query is invalid. It should be as follows:

$QueryResult=msql_query("Select * from `$dbTable`"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. ' and " are not the same in PHP, that's where a big part of your problem lies :).
0

Update your query like $QueryResult=msql_query("Select * from '$dbTable'"); and also

Remove Curly braces inside option, or replace this code

<option value="<?php echo $Row['StateID']; ?>">
                            <?php echo $Row['StateName']; ?>
                        </option>

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.