1

I'm parsing a feed and I want to insert it into my database. I echo out the correct feed entries but when I want to insert into database, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update, link) VALUES (...) on line 1.

Here's the code:

include_once("connect_to_mysql.php");

$xml= simplexml_load_file('http://somefeed/feeds/rss');
$list = $xml->entry;
$title = $list[0]->title;
$img = $list[0]->summary->div->img['src'];
$update = $list[0]->updated;
$link = $list[0]->link['href'];

$sql = mysql_query("INSERT INTO table (title, img, update, link) 
VALUES ('$title', '$img', '$update', '$link')") or die (mysql_error());

This worked fine throughout my website but now I get this error. I'm using xampp. Also some entries are files with http:// that the problem? I found similar posts but their fixes don't work for me.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
denikov
  • 877
  • 2
  • 17
  • 35

1 Answers1

6

the are two reserved keywords: table and update used in your query, it must be escape with backtick.

INSERT INTO `table` (title, img, `update`, link) 
VALUES ('$title', '$img', '$update', '$link')

As a sidenote, the query is vulnerable with SQL Injection if the value(s) came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    Thanks, it worked. Still learning, didn't know about the reserved words. This is parsing a feed so there is no outside data being collected, no input data. I'll move on to mysqli (maybe PDO) when I feel more comfortable with mysql functions. Thanks again. – denikov Jan 11 '13 at 13:35
  • 2
    One more suggestion, next time try **not** to use reserved keywords ;) – Peon Jan 11 '13 at 13:37
  • 1
    Yeah yeah yeah, like I said, I'm still learning. Didn't even know about reserved words. – denikov Jan 11 '13 at 14:22
  • @denikov mistakes are your best teacher `:D` – John Woo Jan 11 '13 at 14:22