问题描述:

I am trying to make a dropdown menu from a database. I am returning nothing right now. For the menu, I need to choose a object then run a SQL query on it. This query will populate a table and will be dynamic.

Here is the code, please help.

<html>

<?php

include('header.php');

?>

<h1>Chemicals Search</h1>

<br/ >

<br/ >

</head>

<h1>

<center>Chemical Search</center>

</h1>

<form action="chemicals.php" method="post">

<label>Search By Product:</label>

<?php

//making the chemical array

$con = mysqli_connect("...");

if (mysqli_connect_errno()) {

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$p = mysqli_query($con, "SELECT product_name FROM product");

$products = mysqli_fetch_array($p);

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

foreach ($product as $key => $value) {

echo "<option value=\"$key\"> $value</option>\n";

}

echo '</select>';

echo "<br>";

?>

</form>

网友答案:

Your Query only selects product name make a little changes to it

 $p = mysqli_query($con,"SELECT product_id,product_name FROM product");
 echo'<select name="product">';
 if(mysqli_num_rows($p) > 0)
 {
  while($products =mysqli_fetch_array($p))
  {
    echo"<option value='".$products['product_id']."'>".$products['product_name']."</option>\n";       
  }
 }
echo '</select>';

echo"<br>";

I am sure this will works, works for me everytime

相关阅读:
Top