This question already has an answer here:
So far I have created the table and getting a successful message. However when I go to insert into the table, I get: Error inserting sample record into 'Users': Column count doesn't match value count at row 1.
This is the code for creating the table:
mysql_select_db($dbName, $dbConnection);
$sql = "CREATE TABLE ".$dbTable." (ID int(6) auto_increment NOT NULL, Username varchar(20) NOT NULL, Password varchar(20) NOT NULL, UserType int(1) NOT NULL, FirstName varchar(15) NOT NULL, LastName varchar(15) NOT NULL, DOB DATETIME NOT NULL, Phone varchar(15) NOT NULL, Department varchar(15),PRIMARY KEY(ID))";
if(mysql_query($sql, $dbConnection))
{
echo("Table '".$dbTable."' created<br />");
}
else
{
echo("Error creating table '".$dbTable."': ".mysql_error()."<br />");
}
And this is the code for inserting:
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Department) VALUES (1, 'admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
if(mysql_query($sql, $dbConnection))
{
echo("Added sample record to '".$dbTable."' table<br />");
}
else
{
echo("Error inserting sample record into '".$dbTable."': ".mysql_error()."<br />");
}
</div>
You forgot to addthe phone number column. The count of your insert values should match the count of the columns you specify in your insert query.
This line is the problem:
$sql = "INSERT INTO ".$dbTable." (Username, //1
Password, //2
UserType, //3
FirstName, //4
LastName, //5
DOB, //6
Department) //7
VALUES (1, //1
'admin', //2
'admin', //3
1, //4
'Admin',//5
'Admin', //6
'1900-01-01', //7
'12345656', //8 You dont have column for this
'IT')"; //9 You dont have column for this
ie, the number of columns and the values are not matching. You have 7 columns and 9 values. So yuo need to add 2 more column names. Also do remember that you dont need to provide the value for auto incremented columns
The correct query may be like this:
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Phone, Department)
VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
Note I have deleted ID as that is auto incremented
You seem to be trying to insert 9 values into 7 columns. In your insert statement, you've left out your Phone
and PRIMARY KEY(ID)
columns
Problem is in your INSERT statement...
Try this
$sql = "INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, Phone , Department) VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";
As error itself says, You are not providing me exact number of values as many you have specified the columns
In short.
('col1', 'col2') VALUES ('val','val2')
Both MUST be same number of columns
In you case, You forgot to add the column name of the value 12345656
You do not add the id in values..
and one phone is missing in the columns
INSERT INTO ".$dbTable." (Username, Password, UserType, FirstName, LastName, DOB, phone, Department) VALUES ('admin', 'admin', 1, 'Admin', 'Admin', '1900-01-01', '12345656', 'IT')";