Iam Zesh
Iam Zesh

Reputation: 1847

mysql: getting ERROR 1054 when using INSERT INTO... SET to populate table

tryin go to create a blog using the tutorials of phpacademy, I ran under a problem when it came to add a category name in my mysql using php (in this video https://www.youtube.com/watch?v=JBcXMVjk83Q)

The author is using this code to put the name of the category into the categories table:

function add_category($name) {
$name = mysql_real_escape_string($name);

mysql_query("INSERT INTO `categories` VALUES `name` = `{$name}`");

}

But it didn't work for me (I am testing it on debian squeeze with PHP Version 5.3.3-7 and Ver 14.14 Distrib 5.1.49).

I am connecting to mysql and to the database correctly but we trying the query directly in mysql via the command line, I have the following error:

ERROR 1054 (42S22): Unknown column 'uncategorized' in 'field list'

I then noticed that using this query was working:

INSERT INTO categories(name)VALUES('uncategorized');

but I wanted to know if there was a way to use the same method as the author used and if it was better, more secure, quicker (or what so ever) than the option that I found to be working.

Thanks!

Upvotes: 1

Views: 4352

Answers (2)

Sonal Khunt
Sonal Khunt

Reputation: 1894

You can do like this

mysql_query("INSERT INTO categories(name) VALUES('$name')");

Upvotes: 1

Minras
Minras

Reputation: 4346

You use wrong quotes here:

mysql_query("INSERT INTO `categories` VALUES `name` = `{$name}`");

Use single quotes:

mysql_query("INSERT INTO `categories` VALUES `name` = '{$name}'");

Single quotes frame data, while backticks ` are used to mark database elements - tables, columns, etc.

Upvotes: 3

Related Questions