0

I'm trying to bring data from MYSQL Database called ebms_db. The table is events and the fields are Event_ID and Event_Name.

The code I'm using currently to show the Event_Name only in the dropdown list is:

    <select name="mySelect"> 
        <?php

            include 'db.php';

            $sql = "SELECT Event_Name FROM events";
            $result = mysql_query($sql);

            echo "<select name='Event_Name'>";
            while ($r = mysql_fetch_array($result)) {
                echo '<option value="'.$row["Event_Name"].'">'.$row["Event_Name"].'</option>';
            }
            echo "</select>";
            ?>

        <input type = "submit" name="Search" value="Search">
    </select>

Db.Php looks like this...

    $servername = "localhost";
    $username = "test";
    $password = "test";
    $dbname = "ebms_db";

    $conn = new mysqli($servername, "test", "test", $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        echo "Error";
    } 

What am I doing wrong?

The output shows a combo box with

 - '.$row["Events_Name"].'

inside it.

3
  • you use while ($r = mysql_fetch_array($result)) {... so instead of $row["Event_Name"] use $r["Event_Name"] Commented Dec 13, 2014 at 0:02
  • possible duplicate of Populate a PHP Dropdown List from MySQL Database Commented Dec 13, 2014 at 0:07
  • I am using $r["Event_Name"] ... I still get the same result. I changed $row to $r ... Commented Dec 13, 2014 at 1:21

3 Answers 3

1
while ($row = mysql_fetch_array($result)) { 
       ^^^^ here
    echo '<option value="'.$row["Event_Name"].'">'.$row["Event_Name"].'</option>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

I changed it to $row in the while loop and in the echo statement. The result is still the same. I get a dropbox with $row["Event_Name"] this written inside it.
1
   <?php

        include 'db.php';
       // you just fetching here (Event_Name) take all the values from the database or 
        $sql = "SELECT * FROM events";
        $result = mysql_query($sql);

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

    <input type = "submit" name="Search" value="Search">

Why are you taking two selects? I just removed one select just beginning above the php tags.

Comments

0

MYSQL is deprecated, you should really use something else, if and/or when they remove it your website will be defunct.

1 Comment

I started coding in PHP and HTML two days ago. So I'm not sure what else I should use. I've gone through many of the solutions on this website but none have helped me.

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.