gpt4 book ai didi

php - 使用

转载 作者:行者123 更新时间:2023-11-30 00:35:53 24 4
gpt4 key购买 nike

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

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

如何从列出的类别(列出的动态)中获取 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']));

并且是否可以有另一个选择表单,其中包含从上面选择的类别中列出的子类别。谨致问候。

最佳答案

您应该在查询中选择 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>

关于php - 使用 <option> 获取 ID 并将其发布到另一个查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com