![]() |
What's wrong?
PHP Code:
Code:
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 '(`u_id`, `username`, `password`, `registerdate`, `registerip`) VALUES ('', 'Tana' at line 1 |
MySQL
UPDATE queries are written differently to INSERT queries. An UPDATE query looks like so:Code:
UPDATE |
PHP Code:
Quote:
|
May I first suggest the use of sprintf to improve readability. As for the query, it seems fine except for the lack of quotes around strings. Try this:
PHP Code:
|
Well, the mysql_escape_string is a bit unneccessary, due to this:
PHP Code:
PHP Code:
|
Okey, I got it working without any errors now..
PHP Code:
PHP Code:
|
OK, the better way to achieve this is to frist execute the INSERT query which will fail on duplicates (assuming of course, you have approprirate PK/unique fields). You then simply determine if the last INSERT actually inserted any data (using mysql_affected_rows), finally, if no data was inserted, you can simply UPDATE the user record. The code should look like this:
PHP Code:
|
Whoa, too advanced for me karl :|
or what, I use that code that you gave, and just insert the UPDATE query inside the if statement and the INSERT query before? |
...Or of course if you wanted to be advanced about this then you could put the
SELECT and the UPDATE into one MySQL statement.Code:
INSERT INTO |
Yes you understood that correct, but as Wildhoney has just pointed out, this can be achieved with just one query, which is definately the way to go if your using MySQL 5 or above.
|
So in my example, it would look like this?
PHP Code:
|
Almost, just a few minor adjustments.
PHP Code:
|
No no, it's not confusing, it's brilliant :D
Just didn't know of it before ^^ Thanks, it's working :D |
| All times are GMT. The time now is 07:37 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0