【问题标题】:Insert failing with SQLITE & PHP PDO - General error: 25 bind or column index out of range使用 SQLITE 和 PHP PDO 插入失败 - 一般错误:25 绑定或列索引超出范围
【发布时间】:2013-03-14 20:51:45
【问题描述】:

我正在尝试使用 Slim 框架中的 PHP PDO 函数将一些数据插入到我的数据库中,但是当我使用 curl 发布时出现以下错误;

SQLSTATE[HY000]:一般错误:25 绑定或列索引超出范围

我使用的 curl 命令是;

curl -i -X POST -H 'Content-Type: application/json' -d '{"category_name":"cat3",     "item_name":"test"}' http://localhost/api/add

有问题的 PHP 函数是;

错误函数

function addContent() {
    $request = \Slim\Slim::getInstance()->request();
    $content = json_decode($request->getBody());
    $sql = "insert into category (category_name) values (:category_name);
    insert into item (category_id,item_name) values 
    ((select category_id from category where category_name = :category_name2),     :item_name);";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("category_name", $content->category_name);
        $stmt->bindParam("category_name2", $content->category_name);
        $stmt->bindParam("item_name", $content->item_name);
        $stmt->execute();
        $db = null;
        echo json_encode($content);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

sql 基本上是在类别表中添加一个新类别,然后在引用新创建的类别的项目表中添加一个新项目。如果我在 sqlite 中运行查询,那么它似乎添加了所需的行就好了,查询是;

insert into category(category_name) values ('cat2');
insert into item (category_id,item_name) values
((select category_id from category where category_name = 'cat2'),'item3');

我为已经存在的类别写了类似的东西,似乎工作正常;

类似的工作函数

function addContent() {
    $request = \Slim\Slim::getInstance()->request();
    $content = json_decode($request->getBody());
    $sql = "insert into item (category_id,item_name) values
((select category_id from category where category_name = :category_name), :item_name);";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("category_name", $content->category_name);
        $stmt->bindParam("item_name", $content->item_name);
        $stmt->execute();
        $db = null;
        echo json_encode($content);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

通过阅读,我相信我需要分别设置两个 :category_name 引用,这就是我有 :category_name 和 :category_name2 的原因。否则,据我所知,这应该按预期工作。非常感谢任何建议。

工作功能由 IMSoP 和 jeroen 的回答提供

这现在可以按要求工作,尽管这可能不是最漂亮的解决方案,但可以完成工作。谢谢大家的建议。

function addContent() {
    $request = \Slim\Slim::getInstance()->request();
    $content = json_decode($request->getBody());
    $sql_category = "insert into category (category_name) values (:category_name);";
    $sql_item = "insert into item (category_id,item_name) values 
((select category_id from category where category_name = :category_name),     :item_name);";
    try {
        $db = getConnection();

        $stmt_category = $db->prepare($sql_category);
        $stmt_category->bindParam("category_name", $content->category_name);
        $stmt_category->execute();

        $stmt_item = $db->prepare($sql_item);
        $stmt_item->bindParam("category_name", $content->category_name);
        $stmt_item->bindParam("item_name", $content->item_name);
        $stmt_item->execute();

        $db = null;
        echo json_encode($content);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

【问题讨论】:

    标签: php sqlite pdo slim


    【解决方案1】:

    您尝试准备的 SQL 字符串包含 2 个 SQL 语句。我不确定,但很可能 SQLite(或 PDO 驱动程序)不支持这一点,所以只会准备其中一个,这意味着另一个中的占位符没有定义。

    尝试将其分成两个单独的语句,每个语句分别准备和执行。

    【讨论】:

    • 谢谢,我现在已经按要求完成了这项工作,请参阅问题的补充内容。
    【解决方案2】:

    问题可能是 PDO 不能在一个查询中执行多个 sql 语句。要查看这是否是问题所在,只需将查询拆分为不同的调用即可。

    如果您的系统满足在 PDO 中运行多个查询的要求,您也可以check here

    【讨论】:

    • 根据标签,这是SQLite,不是MySQL
    • @IMSoP 你说得对,链接/最后一段不适用。
    猜你喜欢
    • 2016-04-30
    • 2014-03-24
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多