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

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

我收到以下错误消息:


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

最佳答案

您有一个印刷错误。您在prepare语句的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)');

关于php - PHP查询在数据库中添加值失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52308126/

10-16 07:56