0

I'm trying to write up a script where the user is able to purchase an amount of points for dollars. I want the transaction to be inserted into MySQL. I keep facing a: "Column count doesn't match value count at row 1" error. And I have no idea what I'm doing wrong.

I have written up this:

mysql_query("INSERT INTO paypal_donations VALUES (NULL, ".$account_id.", ".$char_id.", ".$price.", ".$dp.", NOW(), NOW(), 'Started', 0, 0, '', '');") or die(mysql_error());

But I don't know what to execute in MySQL, since I've never worked with it before.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • 1
    The message you get means that you have more or less columns in the table than the number of values you specify. You can fix this by naming all the columns you wish to insert values into. – user254875486 Oct 28 '12 at 10:46

2 Answers2

3

the reason why you are getting the error is because you are implicitly executing a query that the values supplied in your INSERT statement does not match against the total number of columns on your table.

In order to avoid that, you must supply the column name, ex

INSERT INTO tableName (col1, col2, col3)
VALUES (1,2,3)

if you want to execute query without column name specified, the values must match to the toal number of columns, ex. you have a table that has 4 columns and all of them are integer,

INSERT INTO tableName VALUES (1,2,3,4)
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • +1 I even think you should always specify columns, even in cases where it is not strictly needed. It makes the statement more clear. – GolezTrol Oct 28 '12 at 10:48
  • @GolezTrol I do always specify column names in my query,you'll never know, someone will alter the table by adding extra columns :D hehehe – John Woo Oct 28 '12 at 10:49
  • Thank you, I understand what you said, although like I said: I've never worked with MySQL before. So I have absolutely no idea how to create a table or how to insert data into one. Is it possible for you to write up a quick execution format I can insert into MySQL? – user1780103 Oct 28 '12 at 10:58
  • what kind of query do you mean? query that creates a table? – John Woo Oct 28 '12 at 11:06
  • @user1780103 see here, example of creating a table http://stackoverflow.com/a/12586263/491243 – John Woo Oct 28 '12 at 11:12
1

First of all you should make sure that your variables contain the right values with relevant to SQL syntax. Then, put these variables into the query with the double quote directly. PHP will manipulate the value of them and replace into the query string. E.g.

$account_id = "'Some_ID'"; // there is a single quote
mysql_query("INSERT INTO paypal_donations VALUES (NULL, $account_id, $char_id, $price, $dp, NOW(), NOW(), 'Started', 0, 0, '', '');") or die(mysql_error());

For INSERT SQL command please use syntax:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...) /*Without "..." :) */

If there is null_field or auto_increased_field, use can ignore by not mention it in the query or put value NULL.

Sorry for bad English I wrote.

Han
  • 3,272
  • 3
  • 24
  • 39
  • 1
    Thank you, I understand what you said, although like I said: I've never worked with MySQL before. So I have absolutely no idea how to create a table or how to insert data into one. Is it possible for you to write up a quick execution format I can insert into MySQL? – user1780103 Oct 28 '12 at 10:57
  • Why you don't take a reference to:[w3school](http://www.w3schools.com/php/php_mysql_insert.asp) – Han Oct 28 '12 at 11:02
  • I'm sorry to bother you again, but what do I write infront of that query to create the table: "paypal_donations"? – user1780103 Oct 28 '12 at 11:03
  • For create table, please prefer [http://www.w3schools.com/php/php_mysql_create.asp](http://www.w3schools.com/php/php_mysql_create.asp). I think you should take a skin to all topics about PHP and MySQL there. They are kinda short :) – Han Oct 28 '12 at 11:09
  • I did infact manage to create a table with columns but it still says: "Column count doesn't match value count at row 1" – user1780103 Oct 28 '12 at 11:17
  • Hmm, paste that part of code to PasteBin and share me, I will help you inspect it. I wonder are you double check the diagram (structure) of your database.. – Han Oct 28 '12 at 11:23
  • Bingo, your table (paypal_donations) has only 4 columns (account_id, char_id, price, dp). But your query there are many fields (1 field relevant to 1 column). Your code should be `mysql_query("INSERT INTO paypal_donations VALUES (".$account_id.", ".$char_id.", ".$price.", ".$dp.");") or die(mysql_error());` otherwise you should create more columns for your table. – Han Oct 28 '12 at 11:46