【问题标题】:PHP , PDO , Foreign KeyPHP , PDO , 外键
【发布时间】: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_Name FROM tbl_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'];

标签: php pdo


【解决方案1】:

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

改变这个...

$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'];

【讨论】:

  • 感谢您的合作,但问题是我可以成功地将数据添加到数据库中,检索时出现错误。再次感谢您的努力
  • 上面的代码与向数据库中添加任何内容无关。它用于检索数据并在选择框中使用它。
  • 非常感谢...!
猜你喜欢
  • 1970-01-01
  • 2017-08-27
  • 2012-11-12
  • 2015-12-08
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
相关资源
最近更新 更多