PHP查询在数据库中添加值失败

时间:2018-09-13 06:53:14

标签: php mysql

我收到以下错误消息:

  

警告: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";

所有变量都有一个值并且是正确的。

这是我的桌子:

/* 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;

4 个答案:

答案 0 :(得分:1)

您似乎忘记了在owner处添加冒号。它必须是:owner

答案 1 :(得分:1)

您遇到印刷错误。您忘记了prepare语句ownerVALUES前面的。应该是以下内容:

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

答案 2 :(得分:1)

如警告所述-绑定变量的数量与令牌的数量不匹配。将':'放在'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)
');
...

答案 3 :(得分:0)

快速浏览以下站点: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(?, ?, ?, ?, ?, ?, ?, ?, ?)');