web 2.0

How to use PHP & ListMenu/DropDownList From Database

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

ListMenu/DropDownList From Database

ListMenu/DropDownList From Database

.

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();
?>

ListMenu/DropDownList From Database
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.