php查询失败

vom3gejh  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(240)

我收到以下错误消息:
警告:pdostatement::execute():sqlstate[hy093]:无效参数编号:绑定变量的数量与中的令牌数量不匹配
我仔细检查了我所有的代码,似乎我有正确数量的变量和正确的名称:


# Query to get the variable from the form (different page)

    $name = htmlspecialchars($_POST['name']);
    $description = htmlspecialchars($_POST['description']);
    $region = htmlspecialchars($_POST['region']);
    $country = htmlspecialchars($_POST['country']);
    $market = htmlspecialchars($_POST['market']);
    $strategy = htmlspecialchars($_POST['strategy']);
    $gate   =  htmlspecialchars($_POST['gate']);
    $priority = htmlspecialchars($_POST['priority']);
    $owner = htmlspecialchars($_POST['owner']);

        #Query to add the value (variable in the database)

        $add = $bdd -> prepare('
                               INSERT INTO project(name, 
                                                   description, 
                                                   region_id, 
                                                   country_id, 
                                                   market_id, 
                                                   strategy_id, 
                                                   gate_id, 
                                                   priority_id, 
                                                   owner) 
                               VALUES(:name, 
                                                   :description, 
                                                   :region_id, 
                                                   :country_id, 
                                                   :market_id, 
                                                   :strategy_id, 
                                                   :gate_id, 
                                                   :priority_id, 
                                                   owner)');
        $add->execute(array(
            'name' => $name,
            'description' => $description,
            'region_id' =>  $region,
            'country_id' => $country,
            'market_id' => $market,
            'strategy_id' => $strategy,
            'gate_id' => $gate,
            'priority_id' => $priority,
            'owner' => $owner
            ));

    # verification of the variable
        echo "name: ". $name . " \n";
        echo "description: ". $description . " \n";
        echo "region: ". $region . " \n";
        echo "country: ". $country . " \n";
        echo "market: ". $market . " \n";
        echo "strategy: ". $strategy . " \n";
        echo "gate: " . $gate . " \n";
        echo "priority: " . $priority. " \n";
        echo "owner: " . $owner . " \n";

所有变量都有一个值,并且都是正确的。
这是我的table:

/* CREATION OF TABLE 'concept' */
        CREATE TABLE Project(
            project_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            name TEXT,
            description TEXT,
            region_id SMALLINT UNSIGNED NOT NULL,
            country_id SMALLINT UNSIGNED,
            market_id SMALLINT UNSIGNED,
            strategy_id SMALLINT UNSIGNED,
            gate_id SMALLINT UNSIGNED NOT NULL,
            priority_id SMALLINT UNSIGNED,
            owner VARCHAR(255) NOT NULL,
            CONSTRAINT fk_project_region_id
                    FOREIGN KEY (region_id) 
                    REFERENCES Region(region_id),

            CONSTRAINT fk_project_country_id 
                    FOREIGN KEY (country_id)
                    REFERENCES Country(country_id),

            CONSTRAINT fk_project_market_id 
                    FOREIGN KEY (market_id)
                    REFERENCES Market(market_id),

            CONSTRAINT fk_project_strategy_id
                    FOREIGN KEY (strategy_id)
                    REFERENCES Strategy(strategy_id),

            CONSTRAINT fk_project_gate_id
                    FOREIGN KEY (gate_id)
                    REFERENCES Gate(gate_id),

            CONSTRAINT fk_project_priority_id 
                    FOREIGN KEY (priority_id)
                    REFERENCES priority(priority_id))

            ENGINE=InnoDB;
kg7wmglp

kg7wmglp1#

你好像忘了加冒号 owner . 一定是的 :owner .

qv7cva1a

qv7cva1a2#

正如警告所说-绑定变量的数量与令牌的数量不匹配。放 ':' 之前 'owner' . 您的声明必须是:

...
$add = $bdd -> prepare('
    INSERT INTO project 
        (name, description, region_id, country_id, market_id, strategy_id, gate_id, priority_id, owner) 
    VALUES
        (:name, :description, :region_id, :country_id, :market_id, :strategy_id, :gate_id, :priority_id, :owner)
');
...
r6hnlfcb

r6hnlfcb3#

你有印刷错误。你忘了a:在门前 owner 内部 VALUES 准备陈述的一部分。应包括以下内容:

$add = $bdd -> prepare('
                           INSERT INTO project(name, 
                                               description, 
                                               region_id, 
                                               country_id, 
                                               market_id, 
                                               strategy_id, 
                                               gate_id, 
                                               priority_id, 
                                               owner) 
                                     VALUES(   :name, 
                                               :description, 
                                               :region_id, 
                                               :country_id, 
                                               :market_id, 
                                               :strategy_id, 
                                               :gate_id, 
                                               :priority_id, 
                                               :owner)');
xnifntxz

xnifntxz4#

快速浏览此网站:https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.quickstart.prepared-statements.html
我原以为你应该这样说:

$add = $bdd -> prepare('INSERT INTO project(name, description, region_id, country_id, market_id, strategy_id, gate_id, priority_id, owner) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)');

相关问题