简体   繁体   中英

How to fix column count doesn't match the row value count

I've looked on many different sites including php.net and still haven't found out why my insert script isn't working. I'm using Eclipse to program in PHP and I have the PHP add-on installed so that it works correctly but I haven't figured out what's wrong with my script.

Ex.

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!";
        }
    }
}

You're suppose to be able to select the radio button to insert an object as either an item, weapon, or ability but I keep getting the column value doesn't match the row value count error. Here are the three tables that I created. The ID's are incrementing automatically each time an object is entered. It isn't null but I shouldn't need to include the ID on the form that I'm using to enter the object.

item table:

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

weapon table:

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

ability table:

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

You are using invalid string quotation, which is a simple syntax error in your SQL command.

If you echo this line,

$query = "INSERT INTO ability(AbilityName,Description,Strength,Defense,Luck,Agility) values ('" . $name . '","' . $description . '","' . $strength . '","' . $defense . '","' . $luck . '","' . $agility . "')";

The output will be like:

INSERT INTO ability(AbilityName,Description,Strength,Defense,Luck,Agility) values ('name","description","strength","defense","luck","agility')

As you can see, first and last columns use mixed string quotation which causes the error. I suggest you to use only single quotes in your SQL queries and use HEREDOC method for creating these strings in PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM