1

我查看了许多不同的站点,包括 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 都会自动递增。它不为空,但我不需要在用于输入对象的表单中包含 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
4

1 回答 1

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 中创建这些字符串。

于 2012-11-26T16:06:55.227 回答