我是php的新手,目前正在做我的项目。所以这就是我的问题:
在上面的界面中,我从类别表中的“类别名称”组合框中提取了类别名称和ID。在此表单之后,我应该在'Item表'中输入'category ID',因为Category ID是Item表的外键
这是我添加的代码
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();
}
但这是'item table'的主表。它刚刚添加了查询。我该如何纠正这个问题。我该如何从组合框中取出类别ID并将其输入到项目表中。
答案 0 :(得分:0)
首先,您需要执行select语句
改变这个......
$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语句中的数据创建您的选择框...
<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'];
快乐的编码!