我看过许多不同的网站,包括php.net,但仍然没有找到我的插入脚本无效的原因。我正在使用Eclipse在PHP中编程,我安装了PHP插件,以便它能正常工作,但我还没弄清楚我的脚本有什么问题。
实施例。
switch($type) {
case(1):
require("opendb.php");
$query = "INSERT INTO ability(AbilityName,Description,Strength,Defense,Luck,Agility) values ('" . $name . '","' . $description . '","' . $strength . '","' . $defense . '","' . $luck . '","' . $agility . "')";
if (!mysql_query($query)) {
echo "Ability was not entered successfully: " . mysql_error();
} else {
echo "Ability was entered successfully!";
}
break;
case(2):
require("opendb.php");
$query = "INSERT INTO weapon(WeaponName,Description,Strength,Defense,Luck,Agility) values ('" . $name . '","' . $description . '","' . $strength . '","' . $defense . '","' . $luck . '","' . $agility . "')";
if (!mysql_query($query)) {
echo "Weapon was not entered successfully: " . nysql_error();
} else {
echo "Weapon was entered successfully!";
}
break;
default:
require("opendb.php");
$query = "INSERT INTO item(ItemName,Description,Strength,Defense,Luck,Agility) values ('" . $name . '","' . $description . '","' . $strength . '","' . $defense . '","' . $luck . '","' . $agility . "')";
if (!mysql_query($query)) {
echo "Item was not entered successfully: " . mysql_error();
} else {
echo "Item was entered successfully!";
}
}
}
您可以选择单选按钮将对象作为项目,武器或能力插入,但我一直得到列值与行值计数错误不匹配。这是我创建的三个表。每次输入对象时,ID都会自动递增。它不是null,但我不需要在我用来输入对象的表单上包含ID。
项目表:
ItemID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ItemName varchar(40),
Description longtext,
Strength INT NOT NULL,
Defense INT NOT NULL,
Luck INT NOT NULL,
Agility INT NOT NULL
武器表:
WeaponID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
WeaponName varchar(40),
Description longtext,
Strength INT NOT NULL,
Defense INT NOT NULL,
Luck INT NOT NULL,
Agility INT NOT NULL
能力表:
AbilityID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
AbilityName varchar(40),
Description longtext,
Strength INT NOT NULL,
Defense INT NOT NULL,
Luck INT NOT NULL,
Agility INT NOT NULL
答案 0 :(得分:1)
您使用的是无效的字符串引用,这是SQL命令中的一个简单语法错误。
如果您回显此行,
$query = "INSERT INTO ability(AbilityName,Description,Strength,Defense,Luck,Agility) values ('" . $name . '","' . $description . '","' . $strength . '","' . $defense . '","' . $luck . '","' . $agility . "')";
输出将如下:
INSERT INTO ability(AbilityName,Description,Strength,Defense,Luck,Agility) values ('name","description","strength","defense","luck","agility')
如您所见,第一列和最后一列使用混合字符串引号,这会导致错误。我建议你在SQL查询中只使用单引号,并使用HEREDOC方法在PHP中创建这些字符串。