Page 1 of 1

small sql query problem

Posted: Sun Jan 02, 2005 5:05 am
by kzar
I'm trying to write a query that overwrites / adds a record, but I keep getting this error.

Code: Select all

You have an error in your SQL syntax near 'WHERE `SubjectID`='1' AND `BlockID`='3'' at line 3
Here is my query

Code: Select all

REPLACE INTO `tt_lessons`
SET `TeacherID`='$lessonї$blockid]ї$subjectid]'
WHERE `SubjectID`='$subjectid'
AND `BlockID`='$blockid';

Posted: Sun Jan 02, 2005 6:48 am
by feyd
I'm pretty sure you aren't getting the finalized string you think you are. Try printing it out and seeing what is being sent to the database.

Posted: Sun Jan 02, 2005 9:25 am
by kzar
I am printing already, im pretty sure its all ok. I'm pretty stumped though. I remobed the last two lines of the query that it complains about and then it didn't throw an error so its definately a problem with those.

Posted: Sun Jan 02, 2005 10:45 am
by feyd
oops, I forgot there's no WHERE clauses in [mysql_man]REPLACE[/mysql_man] syntax.

Posted: Sun Jan 02, 2005 2:12 pm
by kzar
damn, I read a little example on this forum with one because I don't really understand the official page. Is there a different way of doing this then?

Posted: Sun Jan 02, 2005 2:14 pm
by feyd
what's wrong with using [mysql_man]UPDATE[/mysql_man]?

Posted: Sun Jan 02, 2005 2:24 pm
by kzar
The problem with update is that it has to exist. I wanted to use replace so that it would make the entry if it existed or not.

Posted: Sun Jan 02, 2005 4:12 pm
by timvw
http://dev.mysql.com/doc/mysql/en/REPLACE.html

->

Code: Select all

REPLACE INTO `tt_lessons`
SET `TeacherID`='$lessonї$blockid]ї$subjectid]', `SubjectID`='$subjectid', `BlockID`='$blockid';