PDA

View Full Version : Replace in MYSQL


terter2000
01-12-2010, 07:53 PM
Hi everyone and every experts,

I am doing a project in FLASH AS3 ,
right now,
i am at the stage where you type in your text in the field it will send to MYSQL.

I want to overwrite/replace if ID is the same as the one in MYSQL

eg , if i post a data with the same USERNAME , it will replace/overwrite all other my sql fields.

i am a newbie in MYSQL , hope anyone can explain clearly what i can do next.

thanks

seco
01-12-2010, 08:09 PM
you can use DUPLICATE KEY UPDATE

Old Pedant
01-12-2010, 08:22 PM
Seco's comment is shorthand for:

INSERT INTO table (username, other, fields)
VALUES( '$username', '$other', '$values' )
ON DUPLICATE KEY
UPDATE table
SET other = '$other', fields = '$values'


Assuming that the username field is your primary key for that table.

I showed PHP usage for the values. You mileage may vary if you aren't using PHP.

terter2000
01-12-2010, 08:30 PM
-code copyright-

Old Pedant
01-13-2010, 12:19 AM
I don't really understand what that code is doing.

It first looks in the entries table to make sure that the username from the form in Flash is in that table.

It dies if the name is not there.

Then only if there is at least one record with that username it inserts another record with that usernam, along with the other info from the form.

HUH?

That doesn't at all match what you say you want.

And of course if the username is not in the table already, it barfs on your feet.

terter2000
01-13-2010, 02:33 AM
thats to show what i have , i dont know what to add on to what i want.

Old Pedant
01-13-2010, 07:13 AM
Sorry...I meant that the code you showed doesn't really match what you said you needed.

The code you showed would barf on your feet if the username is not *ALREADY* in the table. Is that what you want???

terter2000
01-13-2010, 09:03 AM
Sorry...I meant that the code you showed doesn't really match what you said you needed.

The code you showed would barf on your feet if the username is not *ALREADY* in the table. Is that what you want???

not really?
please read this thread
http://codingforums.com/showthread.php?p=909801&posted=1#post909801
thanks a lot !

terter2000
01-13-2010, 05:43 PM
Sorry...I meant that the code you showed doesn't really match what you said you needed.

The code you showed would barf on your feet if the username is not *ALREADY* in the table. Is that what you want???

now i thought of a better example
imagine if i login as "hellokitty"
i decided to change the username to "hellopanda"

how am i going to achieve that , because the above thingy is like what i wanted to do.

isn't it replace in MYSQL?

Old Pedant
01-13-2010, 07:20 PM
First of all, no, it is *NOT* REPLACE. Whoever told you that should be ignored completely.

UPDATE is the proper keyword.

Fine, we can update the username. We can update *anything*.

But I ask again: WHAT do you want to have happen if the username DOES NOT EXIST at all in the table???

Oh...and what about this:
imagine if i login as "hellokitty"
i decided to change the username to "hellopanda"

But what if there is already *another* user named "hellopanda"??

In any case, there's not enough information being sent from the Flash form for the PHP code to figure out that the username has changed.

You would need to *ADD* another field to the form the passed the *ORIGINAL* username to the PHP code.