简体   繁体   中英

pull the data from mysqli to dropdown list php

I have been trying to pull the data from database mysqli in the drop down menu. Here is my code for postad.html. I want to pull categories data from categories table with a row name CatName. I also have CatId that shows ctegory id. Thank you in advance.Please help:

<html>
       <head>
           <title>Post AD</title>
    </head>
  <body>

      <form method="POST" action="postad.php">

     <table>
    <tr>
    <td>AdName:</td>
    <td><input type="text" name="adname"></td>
     </tr>
     <tr>
    <td>AdCategory</td>
         <td>
             //dropdown list
  <select name="Categories">
      <?php include = connect.php 

            $sql = "SELECT CatName FROM categories order by CatName";
            $result = $db->query($sql);
            while($row = $result->fetch_array()) {

        echo "<option value='". $row['CatName']."'>."</option>'; 
   }
    ?>

</select>
  </td>
     </tr>
     <tr>
    <td>Contact Number</td>
    <td><input type="text" name="contactnumber"></td>
     </tr>
     <tr>
    <td>Image</td>
    <td><input type="text" name="image"></td>
     </tr>
     <tr>
    <td>Description</td>
    <td><input type="text" name="description"></td>
     </tr>
        <tr>
    <td>Expiration Date</td>
    <td><input type="text" name="expirationdate"></td>
     </tr>
     <tr>
     <td id="btn"><input type="submit" value='submit' name="submit"></td>
    </tr>    
    </table>     
    </form>
      </body>
</html>

Looks like you have an error with your menu code. Should be this

 echo "<option value=". $row['CatName'] ."></option>'; 

Opposed to

 echo "<option value='". $row['CatName']."'>."</option>'; 

The result from the table is going to be a string regardless so there is no need to worry about value=''. The real problem was how you muffled up your quotes.

EDIT: Just noticed include = connect.php should be include 'connect.php';

<?php
$mysql_host = '';
$mysql_user = '';
$mysql_pass = '';
$mysql_dbase = '';
$mysql_table = '';
$pdo = new PDO('mysql:host=' . $mysql_host . ';dbname=' . $mysql_dbase, $mysql_user, $mysql_pass);
$sth = $pdo->prepare('SELECT * FROM `' . $mysql_table . '` ORDER BY `id` CatName');
$sth->execute();
$result = $sth->fetchAll();
foreach ($result as $row) {?>
    <option value=<?php echo $row['CatName']; ?>></option>; 
<?}?>
echo "<option value='". $row['CatName']."'>."</option>'; 

contains error and also there is no inner html for options

You can use this way for simplicity.

$CatName=$row["CatName"];
echo '<option value="'.$CatName.'">'.$CatName.'</option>'; 

Ok. Now I have created a new php file called getcategories.php. and this page works well. Here is the code for it:

            $sql = "SELECT CatName FROM categories order by CatName";
            $result = $db->query($sql);
            $options="";
//echo "<select value='categories'>";       
while($row = $result->fetch_assoc()) {

        $categoryname=$row["CatName"]; 

   // echo '<option value="'.$categoryname.'">'.$categoryname.'</option>'; 

   // echo "</select>";
   //echo "$categoryname\n";
       $options .= '<option value="'.$categoryname.'">'.$categoryname.'</option>';

}
?>          

How should I pull the data of options to the dropdown of html file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM