0

The database name is sloganstreet and the table is slogan, and the column i'm trying to pull from is called text. Any help would be appreciated. I have established the connection to the database but I can't seem to get the dropdown to populate with the info from mysql. thanks.

<label>Select a slogan:</label>
<select name="select_slogan">

    <?php
    $query = "SELECT text from slogansstreet";
    $result = mysqli_query($conn, $query);
    echo "<select name='Slogan'>";
    echo "<option value='0'>-Select-</option>";
    while($row = mysqli_fetch_array($result)) 
    {
        echo "<option value='".$row['text']."'></option>";
    }
echo "</select>";
?>
23
  • Is your problem that the dropdown is empty, or that there are no elements in it at all? Because you have no text to appear in the dropdown, as there is nothing between <option></option>, you only assign a value (which is hidden to the user). You also likely want mysqli_fetch_assoc instead of mysqli_fetch_array (based on how you use the index). Commented Jun 5, 2016 at 21:38
  • the drop down is empty and it spits out a huge error on my index: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in..." Commented Jun 5, 2016 at 21:40
  • It would be usful to see the error message Commented Jun 5, 2016 at 21:41
  • I tried what you said, but when i remove label, and <select> i lose the dropdown box and get a new error :"arse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in ..." Commented Jun 5, 2016 at 21:43
  • @orangutat Update your question with the code you are actually using, and which line the error is generated on. Commented Jun 5, 2016 at 21:44

1 Answer 1

2

You seem to have done everything twice, but missed putting something in the dropdown that the user can see.

You dont need this line, as you do this in the PHP code as well, so remove it

<select name="select_slogan">

All you need is this

I added some error processing as well, as you seem to be getting a error in that query.

<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

    $query = "SELECT text from slogansstreet";
    $result = mysqli_query($conn, $query);

    if ( $result === FALSE ) {
        echo mysqli_error($conn);
        exit;
    }


    echo "<select name='Slogan'>";
    echo "<option value='0'>-Select-</option>";

    // add MYSQLI_ASSOC as the second param, so you dont get 
    // a numeric and an associative array returned
    // and waste memory
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        echo "<option value='{$row['text']}'>{$row['text']}</option>";
    }
    echo "</select>";
?>
Sign up to request clarification or add additional context in comments.

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.