0

I keep getting "You have an error in your SQL syntax", event if the query runs ok by it's own.

The code :

function CreateUser(){
$token = md5(uniqid(rand(),true));
$query = "USE carshop;
INSERT IGNORE INTO users VALUES(
    '',
    '$GLOBALS[nume]',
    '$GLOBALS[pren]',
    '$GLOBALS[mail]',
    '$GLOBALS[adresa]',
    '$GLOBALS[parola]',
    '$token',
    '0',
    '0',
    '');";
echo $query;
mysql_query($query)or die("Error: ".mysql_error());
break;

}

Echo query : INSERT IGNORE INTO users VALUES( '', 'test1', 'test2', 'dididid@dd.com', 'dsads llaowd sda', '6ccee2f5b01591f6644036c1114b5b4f', '8b3cc272280f13d765b271e203124308', '0', '0', '');

And the DB structure is :

CREATE TABLE users (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`nume` CHAR(20) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
`prenume` CHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
`mail` VARCHAR(30) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
`adresa` TEXT NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
`parola` VARCHAR(100) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
`token` VARCHAR(100) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
`isVerified` INT(1) NOT NULL DEFAULT '0',
`isOnline` INT(1) NOT NULL DEFAULT '0',
`dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

)

Thanks, and sorry if i'm doing any newbie mistakes, i'm new at this

AD7six
  • 63,116
  • 12
  • 91
  • 123
FlatSpeed
  • 51
  • 5
  • Should be using mysqli rather than mysql as it is deprecated and will be removed in future. – Cjmarkham Mar 16 '14 at 19:30
  • Relevant reading: [avoid the dinosaurs](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046#12860046), [don't feed the bears](http://stackoverflow.com/a/60496/2864740) – user2864740 Mar 16 '14 at 19:32
  • Please accept an answer, don't modify the question leaving it unanswered. – AD7six Mar 16 '14 at 20:21

2 Answers2

5

You can't run multiple statements with the mysql_* API. You either need to break them up into multiple statements or, better yet, use the mysqli_* API.

The mysql_* API is deprecated so you shouldn't be using it anyway. And with the mysqli_* API you could use mysqli_multi_query() to run multiple statements at one time.

user2864740
  • 60,010
  • 15
  • 145
  • 220
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

I would suggest doing something like this instead (if you really need to specify the database):

function CreateUser(){
$token = md5(uniqid(rand(),true));
$query = "INSERT IGNORE INTO carshop.users VALUES(
    '',
    '$GLOBALS[nume]',
    '$GLOBALS[pren]',
    '$GLOBALS[mail]',
    '$GLOBALS[adresa]',
    '$GLOBALS[parola]',
    '$token',
    '0',
    '0',
    '');";
echo $query;
mysql_query($query)or die("Error: ".mysql_error());
break;
}