在数据库中添加值的PHP查询失败
我收到以下错误消息: 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:绑定变量的数量与中的令牌数量不匹配 我仔细检查了我所有的代码,似乎我有正确数量的变量和正确的名称:在数据库中添加值的PHP查询失败,php,mysql,Php,Mysql,我收到以下错误消息: 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:绑定变量的数量与中的令牌数量不匹配 我仔细检查了我所有的代码,似乎我有正确数量的变量和正确的名称: # Query to get the variable from the form (different page) $name = htmlspecialchars($_POST['name']); $description = htmlspecial
# 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;
快速浏览此网站: 我原以为你需要这样说:
$add = $bdd -> prepare('INSERT INTO project(name, description, region_id, country_id, market_id, strategy_id, gate_id, priority_id, owner) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)');
快速浏览此网站: 我原以为你需要这样说:
$add = $bdd -> prepare('INSERT INTO project(name, description, region_id, country_id, market_id, strategy_id, gate_id, priority_id, owner) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)');
您似乎忘记在
owner
中添加冒号。它必须是:所有者
您似乎忘记在owner
中添加冒号。它必须是:所有者
您有一个印刷错误。在prepare语句的值部分中,您忘记了在所有者的前面有一个:。它应该是以下内容:
$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)');
您有一个印刷错误。在prepare语句的值部分中,您忘记了在所有者的前面有一个:。它应该是以下内容:
$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)');
正如警告所说-绑定变量的数量与令牌的数量不匹配。将':'
放在'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)
');
...
正如警告所说-绑定变量的数量与令牌的数量不匹配。将':'
放在'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)
');
...
我认为这是键入错误-将“owner”替换为:“owner”。这就是问题所在。非常感谢。我认为这是键入错误-将“owner”替换为:“owner”。这就是问题所在。非常感谢。OP正在使用PDO而不是Mysqli扩展。你的回答对我来说很好。我没有太多的时间来写一个正确的答案,也没有看起来太难我的fopa.OP使用的是PDO而不是Mysqli扩展。你的回答对我来说很好。我没有太多的时间来写一个正确的答案,也没有看起来太难我的fopa很好。@CorentinCanet很乐意帮忙:)@CorentinCanet很乐意帮忙:)