Author Topic: mysql query error - nothing seems wrong?  (Read 346 times)

0 Members and 1 Guest are viewing this topic.

Offline BluMessTopic starter

  • Irregular
  • Posts: 32
    • View Profile
mysql query error - nothing seems wrong?
« on: February 22, 2010, 03:35:53 PM »
Hi

I've gotten really confused about this. Here is a seemingly fine PHP script for sending a query to mysql:

$insert = mysql_query("INSERT INTO ".$new_table_name." VALUES('".mysql_real_escape_string($row['datetime'])."', '', '".mysql_real_escape_string($row['type'])."', '".mysql_real_escape_string($row['title'])."', '".mysql_real_escape_string($row['author'])."', '".mysql_real_escape_string($row['thumbnail'])."', '".mysql_real_escape_string($row['short_d'])."', '".mysql_real_escape_string($row['description'])."', ".$row['width'].", ".$row['height'].", '".mysql_real_escape_string($row['flash'])."', '".mysql_real_escape_string($row['status'])."', ".$row['hits'].", ".$row['favourites'].", ".$row['emails'].")");

Yet, it gives me this error (when inserting some dummy information):

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 'VALUES('2009-08-09 22:23:10', '', 'game', 'testgame', 'testuser', ' ', 'short_description_here', '' at line 1

What's going on here?!
here's the table structure:

CREATE TABLE IF NOT EXISTS `user_games` (
  `datetime` datetime NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(15) NOT NULL DEFAULT 'game',
  `title` varchar(50) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `thumbnail` text,
  `short_d` varchar(60) DEFAULT NULL,
  `description` text,
  `width` int(4) DEFAULT NULL,
  `height` int(4) DEFAULT NULL,
  `flash` text,
  `status` varchar(15) NOT NULL DEFAULT 'pending',
  `hits` int(10) NOT NULL DEFAULT '0',
  `favourites` int(10) NOT NULL DEFAULT '0',
  `emails` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=146 ;

Thanks
« Last Edit: February 22, 2010, 03:40:53 PM by BluMess »

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,586
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: mysql query error - nothing seems wrong?
« Reply #1 on: February 22, 2010, 03:49:59 PM »
It would be to your advantage to build your query statement in a variable (then simply put the variable holding the query into the mysql_query() statement) so that you can echo it to see exactly what it is.

It's likely that the variable $new_table_name is empty.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline BluMessTopic starter

  • Irregular
  • Posts: 32
    • View Profile
Re: mysql query error - nothing seems wrong?
« Reply #2 on: February 22, 2010, 04:14:33 PM »
It would be to your advantage to build your query statement in a variable (then simply put the variable holding the query into the mysql_query() statement) so that you can echo it to see exactly what it is.

It's likely that the variable $new_table_name is empty.
You, sir, are a life saver :D

I put it into a string, echoed it and yes I'd forgotten to global $new_table_name (that query is in a function). Thanks a lot, I'll remember that trick!