【发布时间】:2016-01-08 03:24:22
【问题描述】:
我是 php 新手,目前正在做我的项目。所以这是我的问题:
在上面的界面中,我已经从类别表中提取了类别名称和 ID 到“类别名称”组合框。在这个表格之后,我应该在'Item table'中输入'category ID',因为category ID是Item table的外键
这是我添加的代码
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 并将其输入到项目表中。
【问题讨论】:
-
我看到你没有执行你的第一个查询,一旦你执行了你的查询,那么你将获得类别 ID 并将其存储在变量 $tbl_category_category_ID 中,而不是存储查询
-
您没有从 POST 变量的组合框中获取类别 ID 吗?如果不是,请确保您在 POST 中发送了类别 ID。此外,当您在组合框中对详细信息进行分类时(这意味着您已经在
tbl_category上执行了查询),因此需要在尝试将数据保存到item表时再次运行该查询。 -
你的意思是这样吗?然后它变成这样的错误 category_id,
category_NameFROMtbl_category'; $tbl_category_category_ID = $_POST['$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'];