Jump to content

Accessing MySQL Database from the Web with PhP; drop down menu


jontic

Recommended Posts

I am trying to create a drop down menu with php to select information from a mysql database. With the following query I get a list of the types that need to be in my drop down menu (I do get an error message):

 

echo '<select name="searchtype">';

echo '<option value="">---Select---</option>';

 

$searchtype=$_POST['searchtype'];

$submit=$_POST['submit'];

 

// Database variable declaration

$db_host = "localhost";

$db_user = ".....";

$db_pass = ".....";

$db_name = ".....";

 

// Make DB connection

$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

 

// Check DB connection

if(!$db) {

echo "Error: Could not connect to database. Please try again later.";

exit();

}

 

// Create a php file to display the following query in HTML.

$query1 = "SELECT education FROM users GROUP BY education";

$result1 = mysqli_query($db, $query1);

 

while($row = mysqli_fetch_array($result1))

{

  echo "<option value=\"".$row['education']."\">".$row['education']."\n</option>";

}

 

echo '</select>';

echo '<input type="submit" value="Select" value="submit" />';

 

However, I cannot succeed to get the result (see query below) of the selection made in the drop down menu.

 

$query2 = "SELECT name, surname, education FROM users WHERE education = "$searchtype";

 

Can anyone help me please??

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.