How to use PHP & ListMenu/DropDownList From Database This tutorial how to using PHP & Database fields into html list/menu
ShotDev Focus:
- Using PHP & Listmenu from database.
Example 1
php_listmenu_datebase1.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <? mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("mydatabase"); ?> <body> <form action="php_listmenu_datebase2.php" method="post" name="form1"> List Menu<br> <select name="lmName1"> <option value=""><-- Please Select Item --></option> <? $strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC"; $objQuery = mysql_query($strSQL); while($objResuut = mysql_fetch_array($objQuery)) { ?> <option value="<?=$objResuut["CustomerID"];?>"><?=$objResuut["CustomerID"]." - ".$objResuut["Name"];?></option> <? } ?> </select> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html> <? mysql_close(); ?>
php_listmenu_datebase2.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <? mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("mydatabase"); ?> <body> <? echo $_POST["lmName1"]; echo "<hr>"; $strSQL = "SELECT * FROM customer WHERE CustomerID = '".$_POST["lmName1"]."' "; $objQuery = mysql_query($strSQL); $objResult = mysql_fetch_array($objQuery); echo $objResult["Name"]; ?> </body> </html> <? mysql_close(); ?>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_file_upload1.php
Screenshot
.
Example 2 (Selected default value)
php_listmenu_datebase3.php
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <? $strDefault = "C003"; mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("mydatabase"); ?> <body> <form action="php_listmenu_datebase2.php" method="post" name="form1"> List Menu<br> <select name="lmName1"> <option value=""><-- Please Select Item --></option> <? $strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC"; $objQuery = mysql_query($strSQL); while($objResuut = mysql_fetch_array($objQuery)) { if($strDefault == $objResuut["CustomerID"]) { $sel = "selected"; } else { $sel = ""; } ?> <option value="<?=$objResuut["CustomerID"];?>" <?=$sel;?>><?=$objResuut["CustomerID"]." - ".$objResuut["Name"];?></option> <? } ?> </select> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html> <? mysql_close(); ?>