【问题标题】:Using <option> to get ID and post it in another query使用 <option> 获取 ID 并将其发布到另一个查询中
【发布时间】:2014-04-06 16:26:29
【问题描述】:

我有一个表格可以在表格中插入一些数据。我正在尝试使用 PDO 来做到这一点。 我的数据库中有一个表,其中包含类别和子类别。该表具有以下列:
******
id(自动递增)
parent_id(0 代表类别另一个数字代表子类别)
nume_categorie(带有类别/子类别名称的varchar)
desc_categorie(带有类别描述的 varchar)。

在我的表单中,我有一个选择输入来从 DB 中获取一个类别。

如何从列出的类别(列出的动态)中获取 id?

 <select id="categorie" name="categorie">
 <?php
 $stmt = $db->prepare('Select nume_categorie FROM categories WHERE parent_id=0 order by id');
 $stmt->execute();
 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
 echo '<option>'.$row['nume_categorie'].'</option>';
   }
 ?>
 </select>

我的发帖查询是 $categorie 。 这个变量是:

  $categorie = htmlentities(trim($_POST['categorie']));

如果可能有另一个选择表单,其中列出了从上面选择的类别中列出的子类别。 最好的问候。

【问题讨论】:

    标签: php mysql forms input pdo


    【解决方案1】:

    您应该在查询中选择 id 并使用 $row 数组中的值显示它:

    <select id="categorie" name="categorie">
    <?php
        $stmt = $db->prepare('Select id,nume_categorie FROM categories WHERE parent_id=0 order by id');
        $stmt->execute();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<option value='".$row['id']."'>".$row['nume_categorie']."</option>";
        }
    ?>
    </select>
    

    【讨论】:

    • 好的,但是如何插入选中的id呢? $categorie = htmlentities(trim($_POST['categorie']));如何仅插入选定的 id?这是我的问题
    • 我试过这个,它插入“nume_categorie”而不是id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2016-12-30
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多