Jump to content

Recommended Posts

How is

"INSERT INTO posts (title) VALUES ("  . $_POST['title'] . ");";

 

not the same as INSERT INTO posts (title) VALUES ("test");

 

I'm getting There was an error executing the queryUnknown column 'adfd' in 'field list' when I run the script but if I execute the query by hand it works fine..........

Link to comment
https://forums.phpfreaks.com/topic/257130-sql-query/
Share on other sites

1. You need quotes around the title string.

2. You need to use mysql_real_escape_string on that title first.

3. Don't end your queries with semicolons. This is not the circumstance when they're supposed to be used.

"INSERT INTO posts (title) VALUES ('" . mysql_real_escape_string($_POST['title']) . "')"

Link to comment
https://forums.phpfreaks.com/topic/257130-sql-query/#findComment-1318118
Share on other sites

Semicolons are used to terminate queries in situations where (a) it's not always clear when one is finished, and (b) when there could be more than one query provided. Neither is true with what you provide to mysql_query().

 

Whatever the reason, MySQL themselves have said explicitly that you should not include it:

Normally, the string must consist of a single SQL statement and you should not add a terminating semicolon (";") or \g to the statement.

http://dev.mysql.com/doc/refman/5.5/en/mysql-query.html

Link to comment
https://forums.phpfreaks.com/topic/257130-sql-query/#findComment-1318126
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.