【问题标题】:Selecting categories and subcategories at inserting data into books table在将数据插入书籍表时选择类别和子类别
【发布时间】:2014-01-10 02:59:56
【问题描述】:

我在书店网站上有三张桌子。类别、子类别和书籍我想在将数据插入书籍表时选择类别和相关子类别,但子类别无法从类别中获取 id 并且未在列表中显示任何子类别 我的代码如下:

<th scope="col">Select Category</th>
<th scope="col"><select name="cat">
<?php
include("config.php");
$catsql = "SELECT * FROM categories;";
$catres = mysql_query($catsql);
while($catrow= mysql_fetch_assoc($catres)) {
echo "<option value='" . $catrow['id']
. "'>" . $catrow['cat'] . "</option>";
}
?>

</select></th>
</tr>
<tr>
<th scope="col">Select Subcategory
</th>
<th scope="col"><select name="subcat">
<?php
$cat=$catrow['id'];
$subsql="select * from subcat where catid=$cat;";
$subrs=mysql_query($subsql);
while($subrow=mysql_fetch_array($subrs)){
echo "<option value='" . $subrow['id']
. "'>" . $subrow['subcat'] . "</option>";

}
?>
</select>
</th>

【问题讨论】:

    标签: php mysql select


    【解决方案1】:

    当用户选择任何类别来填充子类别下拉列表时,您应该使用 Ajax 调用。

    【讨论】:

      【解决方案2】:

      我已经使用 java 脚本完成了这项工作:

          <?php
          $db = new mysqli('localhost','root','','sha');
          $query = "SELECT * FROM categories;";
          $result = $db->query($query);
      
        while($row = $result->fetch_assoc()){
          $categories[] = array("id" => $row['id'], "val" => $row['cat']);
        }
      
        $query = "SELECT id, catid, subcat FROM subcat";
        $result = $db->query($query);
      
        while($row = $result->fetch_assoc()){
          $subcats[$row['catid']][] = array("id" => $row['id'], "val" => $row['subcat']);
        }
      
        $jsonCats = json_encode($categories);
        $jsonSubCats = json_encode($subcats);
      
      
      ?>
      
      <!docytpe html>
      <html>
      
        <head>
          <script type='text/javascript'>
            <?php
              echo "var categories = $jsonCats; \n";
              echo "var subcats = $jsonSubCats; \n";
            ?>
            function loadCategories(){
              var select = document.getElementById("categoriesSelect");
              select.onchange = updateSubCats;
              for(var i = 0; i < categories.length; i++){
                select.options[i] = new Option(categories[i].val,categories[i].id);          
              }
            }
            function updateSubCats(){
              var catSelect = this;
              var catid = this.value;
              var subcatSelect = document.getElementById("subcatsSelect");
              subcatSelect.options.length = 0; //delete all options if any present
              for(var i = 0; i < subcats[catid].length; i++){
                subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].id);
              }
            }
          </script>
      
        </head>
      
        <body onload='loadCategories()'>
        <form action="storebooks.php" method="post"><table><tr><th scope="col">Select Category</th>
          <th scope="col"><select id='categoriesSelect' <select name="cid">
          </select></th>
        </tr>
      <tr>
          <th scope="col">Select Subcategory
          </th>
          <th scope="col">
          <select id='subcatsSelect' name="subcatid">
          </select></th>
        </tr>
        <tr>
          <th scope="col"><p>Title</p></th>
          <th scope="col"><input name="title" type="text" /></th>
        </tr>
        <tr>
          <th scope="col">Author</td>
          <th scope="col"><input type="text" name="author" id="author" /></td>
        </tr>
        <tr>
          <td>description</td>
          <td><textarea name="bookdesc" cols="30" rows="15"></textarea></td>
        </tr>
        <tr>
          <th scope="col"><p>keywords</p></th>
          <th scope="col"><input name="keywords" type="text" /></th>
        </tr>
         <tr>
          <th scope="col"><p>Image</p></th>
          <th scope="col"><input name="image" type="text" /></th>
        </tr>
        <tr>
          <th scope="col"><p>Order</p></th>
          <th scope="col"><input name="order" type="text" /></th>
        </tr>
          <tr>
          <td></td>
          <td><input name="submit" type="submit" value="submit" id="submit" /></td>
        </tr>
      </table>
      </form>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 2020-05-06
        • 2019-08-28
        相关资源
        最近更新 更多