首页 > 解决方案 > 在数据库中添加值的 PHP 查询失败

问题描述

我收到以下错误消息:

警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: 绑定变量的数量与标记的数量不匹配

我仔细检查了所有代码,似乎我确实有正确数量的变量和正确的名称:

    # 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;

标签: phpmysql

解决方案


您似乎忘记在owner. 一定是:owner


推荐阅读