PHP/ MySQL Drop Down Lis

Discussion in 'PHP' started by BANAGO, Jan 4, 2009.

  1. #1
    Hi guys!

    I am developing my first plugin, but my PHP is still primitive even thought I am learning very fast.

    I what to have a dropdown list which will be let's say:
    1. apples
    2. pears
    3. bananas

    Is there a MySQL option to have only one of them selected in a dropdown menu? If yes, which is it? If no, how can this be done in PHP.

    Thanks very much!
     
    BANAGO, Jan 4, 2009 IP
  2. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Well, MySQL is only a database, so I don't really understand how you will use mysql to select only one of the items.

    You highlight a drop down item with selected="selected" (you can't use SELECTED anymore, as it does not follow the standards).

    So:
    <select>
    <option>Apples</option>
    <option selected="selected">Pears</option>
    <option>Bananas</option>
    </select>

    So, now "Pears" will be selected as the page loads.

    Let's say you have created a new mysql table called "fruits".
    |id| |name|
    1 Apples
    2 Pears
    3 Bananas

    Then you can create a drop down from this table with this code:
    
    <?
    //Your database connection
    
    $fruits = mysql_query("SELECT * FROM fruits"); //Get all fruits from the database
    $selectedFruit = "Apples"; //Which fruit should be selected as standard
    echo "<select>"; //Start our drop down list
    while($fruit = mysql_fetch_array($fruits)){ //Loop through all fruits, create an item for every fruit.
    	if($fruit[name] == $selectedFruit){ //If the current fruit in the loop is the one we want to select it
    		$selected = ' selected="selected"';
    	}
    	echo "<option".$selected.">".$fruit[name]."</option>"; //Create the item
    } //End the loop
    echo "</select>";
    ?>
    
    PHP:
    Hope it helped you! :)
     
    elias_sorensen, Jan 4, 2009 IP
  3. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Yes, It helped me a lot, thanks!

    Now, I want to know how to do the other way around. Let's say I want to visitors to select which is their favorite fruit. The fruit that they will select will be stored in the database. After stored, the will be shown only as the selected option, not in a drop down menu. How to do that?

    Thanks very very much!
     
    BANAGO, Jan 4, 2009 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    You don't have to save it in the database to show their selected fruit.

    <?
    if(isset($_POST[fruit])){ //If the user has selected a fruit and clicked "Next"
    	echo $_POST[fruit]; //Write the fruit they've chosen
    }
    ?>
    <form action="" method="post">
    Select your favorite fruit: 
    	<select name="fruit">
    		<option value="Apples">Apples</option>
    		<option value="Pears">Pears</option>
    		<option value="Bananas">Bananas</option>
    	</select><br />
    	<input type="submit" value="Next" />
    </form>
    
    PHP:
    You can also save the favorite fruit into a session. This allows you to get the fruit at other pages (you use sessions in login systems etc.).
    Everything you set in a session will remain active until the browser closes (then the session will be destroid).

    <?
    session_start(); //Start the session. This should ALWAYS be done as the first thing in your code. Otherwise it will retun errors.
    
    if(isset($_POST[fruit])){ //If the user has selected a fruit and clicked "Okay"
    	$_SESSION[favoritefruit] = $_POST[fruit]; //We put the favorite fruit into a session variable
    	echo "Your favorite fruit has been put into a session variable. Try navigate to the other file you've made to echo the fruit.";
    }
    ?>
    <form action="" method="post">
    Select your favorite fruit: 
    	<select name="fruit">
    		<option value="Apples">Apples</option>
    		<option value="Pears">Pears</option>
    		<option value="Bananas">Bananas</option>
    	</select><br />
    	<input type="submit" value="Okay" />
    </form>
    
    PHP:
    Then create another file to display that session variable:
    
    <?
    session_start(); //Start the session again, otherwise you can't access the variable
    echo $_SESSION[favoritefruit]; //Print the favorite fruit
    ?>
    
    PHP:
    Some quick database theory, to make you understand them better.

    - You do always have to start a connection before you're trying to interact with the database :)
    - Every interactio with the database uses the function mysql_query().
    - Get some data from a database: mysql_query("SELECT * FROM row WHERE id = '2' OR id = '3' ORDER BY date DESC");
    - Update some data: mysql_query("UPDATE row SET fruit = 'Apples' WHERE fruit = 'Bananas'");
    - Insert some data: mysql_query("INSERT INTO row (fruit,column2) VALUES('Banana','Something else')");

    Hope this helped you :)

    Oh.. and remember.. if you put ANY inputs ($_POST and $_GET) into the database, don't forget to escape them. Otherwise the user can make so-called sql injections (google it).

    This is done e.g.:
    mysql_query("INSERT INTO fruits (fruit) VALUES('".mysql_real_escape_string($_POST[fruit])."')");
    :)
     
    elias_sorensen, Jan 4, 2009 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    You also need to escape any user data output to screen (XSS attacks mainly.)

    Also, use quotations when referencing array keys, without is incorrect unless you want the constant functionality.

    $_POST[fruit] -> $_POST['fruit'] instead.
     
    Danltn, Jan 4, 2009 IP