Jump to content

populate drop down menu from mysql using php


Recommended Posts


I have a code where I populate the drop down list base on one of the columns in my database. It already display but the value of the chosen list did not affect my mysql query. Now I wanted to ask how I can put the value of the list to my mysql query.
This is my code:
 <?php// db connection$db = "mds_reports";[/indent]if($connect = mysql_connect("", "mds_reports", "password"))   $connect = mysql_select_db($db);	else die("Unable to connect".mysql_error());$date_from = $_POST['dfrom'];$date_to = $_POST['dto'];?><html><head><title>Cancelled Orders </title></head><body>  <h1>Cancelled Orders</h1>  <form name="cancelled_orders" method="POST" action="cancelled_orders_index.php">   Date From: <input type="text" name="dfrom" id="dfrom"> To: <input type="text" name="dto" id="dto"><br /><br />   <?php	$i = $_POST['name'];	$result_query = mysql_query ("SELECT name FROM restaurant_master") or die (mysql_error());		echo "<select name='name' value=''>Restaurant Name</option>";		$i=1;	while($nt=mysql_fetch_assoc($result_query))	{	echo "<option selected value='$i' > $nt[name] </option>"."<BR>";	$i++;	}	echo "</select>";   ?>   <input name="submit" type="submit" value="Go">  </form>  <table>  <table border="2"  cellpadding="2"  cellspacing="1"  style='width:100%;'>  <tr>   <th>Restaurant Code</th>   <th>Restaurant Name</th>   <th>Order No</th>   <th>Order Source</th>   <th>Payment Type</th>   <th>Net Total</th>   <th>Gross Total</th>   <th>Reason</th>  </tr>  <?php   if(isset($_POST['submit'])){   echo "Date select from $date_from to $date_to"."</p>";   echo "Restaurant: $i"."</p>";     $sql = "Select restaurant_master.code, restaurant_master.name,	  mds_orders.OrderNo, mds_orders.OrderSourceText AS 'Order Source',	  mds_orders.PaymentTypeText AS 'Payment Type', mds_orders.NetTotal,	  mds_orders.GrossTotal, ReasonMaster.ReasonDescription AS 'Reason' 	 FROM mds_orders	  INNER JOIN ReasonMaster	   ON mds_orders.ReasonFKID = ReasonMaster.PKID	  INNER JOIN restaurant_master	   ON mds_orders.RestaurantID = restaurant_master.pkid 	 WHERE mds_orders.OrderDate BETWEEN '".$date_from." 00:00:00' AND '".$date_to." 23:59:59'	  AND mds_orders.RestaurantID LIKE '$i'	  AND mds_orders.StatusFKID = 3 	 ORDER BY restaurant_master.code";   echo $sql;	$result = mysql_query($sql)			   or die("Error: ".mysql_error());	$num_rows = mysql_num_rows($result);	$i = 0;	 if (mysql_num_rows($result) >0) {	 while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){	  $csv .= $row['code'].",".		$row['name'].",".		$row['OrderNo'].",".		$row['Order Source'].",".		$row['Payment Type'].",".		$row['NetTotal'].",".		$row['GrossTotal'].",".		$row['Reason'].","."<br>";  ?>	<tr>	 <td><?php echo $row['code'];?></td>	 <td><?php echo $row['name'];?></td>	 <td><?php echo $row['OrderNo'];?></td>	 <td><?php echo $row['Order Source'];?></td>	 <td><?php echo $row['Payment Type'];?></td>	 <td><?php echo $row['NetTotal'];?></td>	 <td><?php echo $row['GrossTotal'];?></td>	 <td><?php echo $row['Reason'];?></td>	</tr>	 <?php	  }}}  ?>   </table><br />   <form name="export" method="POST" action="cancelled_orders_export.php">	  <input type="hidden" name="cancel_dfrom" id="data" value="<?=$date_from?>">   <input type="hidden" name="cancel_dto" id="data" value="<?=$date_to?>">   <input type="hidden" name="cancel_name" id="data" value="<?=$name;?>">	  <input type="submit" name="extract" value="Extract">	  </form></body></html>

Thanks and best regards.
Link to comment
Share on other sites

Values of dropdown lists are sent to PHP in the $_GET or $_POST arrays. You can then put their value into the query like any other variable. If something's not working, the first thing to do is check what value the $_GET or $_POST element has.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...