【问题标题】:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in add_product.php on line 29 [duplicate]PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in add_product.php on line 29 [重复]
【发布时间】:2023-03-18 22:14:01
【问题描述】:

很抱歉,对 PHP 和 mySQL 很陌生。

这是错误所指的代码。

$query = 'INSERT INTO movies
            (title, year, actor, notes, category)
          VALUES
            (:code, :name, :price, :notes, :category_id)';
$statement = $db->prepare($query);
$statement->bindValue(':title', $code);
$statement->bindValue(':year', $name);
$statement->bindValue(':actor', $price);
$statement->bindValue(':notes', $notes);
$statement->bindValue(':category', $category_id);
$statement->execute();
$statement->closeCursor();

错误是指execute();陈述 任何帮助都会很棒。

【问题讨论】:

    标签: php mysql dbo


    【解决方案1】:

    我认为它应该是这样的,具有适当的变量绑定,你在这样做时使用了错误的名称。让我们做吧-

    $query = 'INSERT INTO
                movies(title, year, actor, notes, category)
              VALUES 
                (:code, :name, :price, :notes, :category_id)';
    
    $statement = $db->prepare($query);
    $statement->bindValue(':code', $code); // see the changes here 
    $statement->bindValue(':name', $name); // see the changes here 
    $statement->bindValue(':price', $price); // see the changes here 
    $statement->bindValue(':notes', $notes);// see the changes here  
    $statement->bindValue(':category_id', $category_id); // see the changes here 
    $statement->execute();
    $statement->closeCursor();
    

    PDOStatement::bindValue — 将值绑定到参数。

    参数

    参数标识符。对于使用命名的准备好的语句 占位符,这将是 :name 形式的参数名称。为一个 使用问号占位符准备好的语句,这将是 参数的 1 索引位置。

    价值

    要绑定到参数的值。

    但在您的情况下,您在使用 bindValue() 时使用了不同的名称。查看更多http://php.net/manual/en/pdostatement.bindvalue.php

    【讨论】:

    • 我正在课堂上做一个项目,并从另一个标题不同的项目中提取这个项目。您对“:代码”所做的更改是否会发生? $代码);仍然可以使用上述插入电影的语句(标题、年份、演员、注释、类别),还是我还需要将它们更改为代码、名称、价格等?
    • @jriding mate 我没有正确理解你的意思。让我编辑我的答案并添加更详细的解释,让您更清楚。
    • 再次感谢您的时间和解释。我想我可能必须检查所有代码并确保所有内容都与数据库列匹配,这样我就不会因拼写错误而出错。
    • 我相信我明白参考网址在说什么。我将通过我的代码并清理它,如果我仍然有问题将重新发布。再次感谢您提供的信息。
    猜你喜欢
    • 2018-04-22
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    相关资源
    最近更新 更多