gpt4 book ai didi

PHP , PDO , 外键

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:33 27 4
gpt4 key购买 nike

我是 php 的新手,目前正在做我的项目。所以这就是我的问题:

enter image description here

在上面的界面中,我从类别表中提取类别名称和 ID 到“类别名称”组合框。在这个表格之后,我应该在“项目表”中输入“类别 ID”,因为类别 ID 是项目表的外键

这是我添加的代码

require_once '../../config/config.php';

$query = 'SELECT `category_id`, `category_Name` FROM `tbl_category`';

$tbl_category_category_ID = $query;
$item_name = $_POST['item_name'];
$price = $_POST['price'];
$tbl_distributor_distributor_ID= $_POST['tbl_distributor_distributor_ID'];
$item_lowlevel = $_POST['item_lowlevel'];
$status = $_POST['status'];

try {


$sql = "INSERT INTO `tbl_item`(`tbl_category_category_ID`, `item_name`, `price`, `tbl_distributor_distributor_ID`, `item_lowlevel`,`status`)
VALUES (:tbl_category_category_ID, :item_name, :price, :tbl_distributor_distributor_ID, :item_lowlevel ,:status)";
$qry = $conn->prepare($sql);
$qry->execute(array(':tbl_category_category_ID' => $tbl_category_category_ID,
':item_name' => $item_name,
':price' => $price,
':tbl_distributor_distributor_ID' => $tbl_distributor_distributor_ID,
':item_lowlevel' => $item_lowlevel,
':status' => $status));

$conn = null;
} catch (PDOException $e) {
echo $e->getMessage();
}

但这是‘元素表’的主表。它刚刚添加了查询。我该如何纠正这个问题。我应该如何从组合框中获取类别 ID 并将其输入到项目表中。

enter image description here

最佳答案

首先你需要执行你的选择语句

改变这个...

$query = 'SELECT `category_id`, `category_Name` FROM `tbl_category`';

为此...

$sql = "SELECT category_id, category_Name FROM tbl_category ORDER BY category_Name ASC";
$query = $conn->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);

接下来使用来自您的选择语句的数据创建您的选择框...

<select name="category_id" id="category_id">
<option value="">Select A Category</option>
<?php do { ?>
<option value="<?php echo $row['category_id'] ?>"><?php echo $row['category_Name'] ?></option>
<?php } while ($row = $query->fetch(PDO::FETCH_ASSOC)); ?>
</select>

终于得到你的类别id
改变这个...

$tbl_category_category_ID = $query;

为此...

$tbl_category_category_ID = $_POST['category_id'];

关于PHP , PDO , 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050795/

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