Hi I'm very new into this website and to PHP.
I want to make a drop down to display from my database. when i wrote the option value, a drop down is displaying in my html page however all the data comes together in one drop down. I want to make them in three separate that's Car_ID, Registration_number and make drop down and a submit button.

<html>
   <h1>  Online car sale garadge. </h1>
   <h2>This website is designed to search and buy cars online </h2> 
   	<body bgcolor="33EE77">
   	<img src="pink.jpg" width="340" height="250" />
   </style>
   </head>
   <body>


<?php

$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'car sale project'; //Replace with your database name
$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$dbconn) 
  {die('Error connecting to the DB!');}
	if ($dbconn) 
  		{echo('<p>Please select the car you want to buy.</p>');}
			if (!mysql_select_db($dbname))  
 			 {die( '<p>Unable to locate the main database.</p>');} 
		if (mysql_select_db($dbname))  
 	 {echo('<p>You are now connected to our database.</p>');}
$query = "SELECT * from cars";
$result = mysql_query($query);
?>

<select name="cars">
<?php
  while ($row=mysql_fetch_array($result))
  {
?>
<tr>
<tb>
 <option value="" selected>Select </option>
 
  <option value="Cars_ID "><?php echo($row['Cars_ID']);?></option></br> 
  <option value="Registration_number "><?php echo($row['Registration_number']);?></option></br>
  <option value=" "><?php echo($row['Make']);?></option></br>  
</tb>
  </tr>
<?php
  }
?>
</select>

</td>
</tr>
</table>

</div>
</body>
</html>

Thank you for any suggestion! Abe

Recommended Answers

All 12 Replies

I am adding a bit information to make it easy for you, the problem I have is from number 38 to 42, however if you spot any other it would be great full if you can tell me how to modify them.

thanks in advance Abe

Try this:

<select name="cars">
<?php
  while ($row=mysql_fetch_array($result))
  {
?>
<tr>
<tb>
 <option value="" selected>Select </option>
 
  <option value="cars_id"><?php echo $row['cars_id'] ;?></option></br> 
  <option value="registration_number"><?php echo $row['registration_number'] ;?></option></br>
  <option value="make"><?php echo $row['make'] ;?></option></br>  
</tb>
  </tr>
<?php
  }
?>
</select>

But what are you trying to accomplish with the drop down menus? Because in order to use the data the user selects you will need to do this:

//Sanitize input data
$cars_id = mysql_real_escape_string($_POST['cars_id']);
$registration_number = mysql_real_escape_string($_POST['registration_number']);
$make = mysql_real_escape_string($_POST['make']);

Then you can use the data in those variables for whatever you want.

Thanks Tekkno for your replaying me
I have tried the changes you have made, however in my database the fields names are exactly the same as I have posted, when I change in lower case (that you make the change) I have got an error on my html page.
Actually I want to display the three drop downs onto my html to enable for customer to choose cars from my database. I am using uniserver phpMyAdmin. then I have three fields name, Cars_ID, Registration_number and Make.

thanks in advance Abe

What was the error you received? You can change them back. I would also change this:

while ($row=mysql_fetch_array($result))

To this:

while ($row=mysql_fetch_assoc($result))

This will give you an associative array, which in my opinion is easier to work with.

That helped me a lot thanks Takkno.
Now I want to allow users to search cars from my database. However when I hit the search button I got an error "Forbidden
You don't have permission to access /temp_folder/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>H:/UniServer/www/temp_folder/opendb.php</b> on line <b>77</b><br /> on this server."

Here is my updated code.

<html>
   <h1> online car sale garadge. </h1>
   <h2>This website is designed to search and buy cars online </h2> 
   	<body bgcolor="33EE77">
   	<img src="pink.jpg" width="340" height="250" />
   </style>
   </head>
   <body>


<?php

$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'car sale project'; //Replace with your database name
$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$dbconn) 
  {die('Error connecting to the DB!');}
	if ($dbconn) 
  		{echo('<p>Please select the car you want to buy.</p>');}
			if (!mysql_select_db($dbname))  
 			 {die( '<p>Unable to locate the main database.</p>');} 
		if (mysql_select_db($dbname))  
 	 {echo('<p>You are now connected to our database.</p>');}
$query = "SELECT * from cars";
$result = mysql_query($query);
?>

<form method="post" action="<?php echo $PHP_SELF;?>"> 


<select name = "Make">
  <?php
  while ($row=mysql_fetch_array($result))
    {
?>
   <option value=""selected>Select make </option>
   <option value= "<?php echo $row['Make'];?>"><?php echo $row['Make'];?></option>
 <?php
 }
 ?>
</select>

<?php
$query = "SELECT * from cars";
$result = mysql_query($query);
?>

<select name "Model">
  <?php
   while($row=mysql_fetch_array($result))
     {
     ?>
 <option value="" selected> Select model </option>
 <option value= "<?php echo $row['Model'];?>"><?php echo $row['Model'];?></option>
     <?php
     }
     ?>
     </select>
     
     <?php
     $query = "SELECT * from cars";
     $result = mysql_query($query);
     ?>
     
     <select name = "Colour">
       <?php
       while ($row=mysql_fetch_array($result))
         {
     ?>
        <option value=""selected>Select colour </option>
        <option value= "<?php echo $row['Colour'];?>"><?php echo $row['Colour'];?></option>
      <?php
      }
      ?>
</select>

   <?php
   
   $Make = $_POST["Make"];
   $Model = $_POST["Model"];
   $Colour = $_POST["Colour"];
   ?>
   
<input type ="submit" value= "Search">
</post>
</body>
</html>

Best regards Abe

It looks like your syntax is wrong on line 31. PHP_SELF is a super global, it should be:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Let me know how that works out.

Sorry that does work, the error message has gone but nothing else. it remain the same while I clicked onto the search button it just refreshing the page. lol

thank you anyway for trying to help me

Sorry that does work, the error message has gone but nothing else. it remain the same while I clicked onto the search button it just refreshing the page. lol

thank you anyway for trying to help me

All is not lost, I added some error handling on the query. Seems that your query may be failing. But you definitely need better error handling throughout the script, otherwise you will not know where it is failing. Are your drop down menu's generating data? Is it echoing that you are connected to the database? If there are any errors now let me know. I also noticed that your option tags don't have any values, therefore it won't process anything when you press search. You need unique values there.

<html>
   <h1> online car sale garage. </h1>
   <h2>This website is designed to search and buy cars online </h2> 
   	<body bgcolor="33EE77">
   	<img src="pink.jpg" width="340" height="250" />
   </style>
   </head>
   <body>


<?php

$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'car sale project'; //Replace with your database name
$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$dbconn) 
  {die('Error connecting to the DB!');}
	if ($dbconn) 
  		{echo('<p>Please select the car you want to buy.</p>');}
			if (!mysql_select_db($dbname))  
 			 {die( '<p>Unable to locate the main database.</p>');} 
		if (mysql_select_db($dbname))  
 	 {echo('<p>You are now connected to our database.</p>');}
$query = "SELECT * from cars";
if(!$res = mysql_query($query)) {
echo mysql_error();
}else{
$result = $res;
}
?>

<form method="post" action="<?php echo $PHP_SELF;?>"> 

<select name = "Make">
  <?php
  while ($row=mysql_fetch_assoc($result))
    {
?>
   <option value=""selected>Select make </option>
   <option value= "<?php echo $row['Make'];?>"><?php echo $row['Make'];?></option>
 <?php
 }
 ?>
</select>

<select name "Model">
  <?php
   while($row=mysql_fetch_assoc($result))
     {
     ?>
 <option value="" selected> Select model </option>
 <option value= "<?php echo $row['Model'];?>"><?php echo $row['Model'];?></option>
     <?php
     }
     ?>
     </select>
     
     <select name = "Colour">
       <?php
       while ($row=mysql_fetch_assoc($result))
         {
     ?>
        <option value=""selected>Select colour </option>
        <option value= "<?php echo $row['Colour'];?>"><?php echo $row['Colour'];?></option>
      <?php
      }
      ?>
</select>
   
<input type ="submit" value= "Search">
</body>
</html>

well, when I added the changes you have made I have lost two drop down. then I undo it as it was. The php has successfully connect to my database and it has displayed the values from my table, but when I click the search button it remain the same nothing changes has happened. I reckon the error is with the "POST" method. In addition in the drop down it displays "select make, BMW, select make, Toyota, select make ... etc." the select is keep repeating. what could the problem to make that?
thanks again for your help.

Well the reason I changed the code the way I did, is because you are running the same query 3 times and you shouldn't need to. If that didn't work, you may need to change each of your variables to something unique for each while loop so they are not conflicting. For instance, possibly the $row variable. But in order to get the correct keys and values from your array, it depends on the layout of your database. It would typically be something like $row. If the errors are in the POST you would not know because there is no error handling as I mentioned before. You need to implement some conditional statements that will throw an error if it fails, such as isset. Also, did you add values like I recommended? I believe the reason "select make" is repeating is because you have it inside your while loop.

<select name = "Make">
  <?php
  while ($row=mysql_fetch_assoc($result))
    {
?>
   <option value=""selected>Select make </option>
   <option value= "<?php echo $row['Make'];?>"><?php echo $row['Make'];?></option>
 <?php
 }
 ?>
</select>

Try it outside the while loop.

<select name = "Make">
<option value=""selected>Select make </option>
  <?php
  while ($row=mysql_fetch_assoc($result))
    {
?>
   <option value= "<?php echo $row['Make'];?>"><?php echo $row['Make'];?></option>
 <?php
 }
 ?>
</select>

Hope this helps.

Thanks again Tekkno
Now all the drop downs are working fine and nice, the only problem i have now is with the post method. is there's any alternate method which can I use instead the Post?
tnx

When you say it is not posting what are you expecting it to do? Put their selection in the database? Or is it supposed to display a page with the cars? Have you already done something like this?

$cars_id = mysql_real_escape_string($_POST['cars_id']);
$registration_number = mysql_real_escape_string($_POST['registration_number']);
$make = mysql_real_escape_string($_POST['make']);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.