简体   繁体   English

将数据从mysqli拉到下拉列表php

[英]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. 我一直在尝试从下拉菜单中的数据库mysqli中提取数据。 Here is my code for postad.html. 这是我的postad.html代码。 I want to pull categories data from categories table with a row name CatName. 我想从具有行名CatName的类别表中提取类别数据。 I also have CatId that shows ctegory id. 我也有CatId显示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=''. 该表的结果将是一个字符串,因此无需担心value =''。 The real problem was how you muffled up your quotes. 真正的问题是您如何低估报价。

EDIT: Just noticed include = connect.php should be include 'connect.php'; 编辑:刚注意到include = connect.php应该是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 包含错误,也没有内部html选项

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. 现在,我创建了一个名为getcategories.php的新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 我应该如何将选项数据拉到html文件的下拉列表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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