newbie q: update and/or insert?

newbie q: update and/or insert?

am 13.04.2006 15:40:49 von dx27s

Sorry for the newbie question here... but I haven't been able to figure
this out searching the web.

I'm using MySQL with PHP. I want to update a record if it already
exists. If it does not exist, I need to create a new record.

Is this done through a simple SQL command, or do I need to use PHP to
negotiate two separate UPDATE and INSERT statements?

Re: newbie q: update and/or insert?

am 13.04.2006 16:03:17 von Pierre Gilquin

You are looking for an object-relationnal mapping. This can do that (and
much more). But PHP is a beginner with objects. For now, this can be found
in Java, C++ languages only.

Pierre

"dx27s" a écrit dans le message de news:
Bxs%f.10345$1q4.3243@bgtnsc05-news.ops.worldnet.att.net...
> Sorry for the newbie question here... but I haven't been able to figure
> this out searching the web.
>
> I'm using MySQL with PHP. I want to update a record if it already exists.
> If it does not exist, I need to create a new record.
>
> Is this done through a simple SQL command, or do I need to use PHP to
> negotiate two separate UPDATE and INSERT statements?

Re: newbie q: update and/or insert?

am 13.04.2006 18:29:36 von Bill Karwin

dx27s wrote:
> I'm using MySQL with PHP. I want to update a record if it already
> exists. If it does not exist, I need to create a new record.

This can be done either with the REPLACE statement, or the INSERT...ON
DUPLICATE KEY UPDATE. These two statements are subtly different.

REPLACE literally deletes an existing record and inserts a new one in
its place, using the values you specify. This means you need to provide
values for all the columns in your statement.

INSERT...ON DUPLICATE KEY UPDATE allows you to modify individual columns
in the case of an existing record, and leave the others alone. This was
added in MySQL 4.1.

Read the docs for more information:
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.h tml
http://dev.mysql.com/doc/refman/5.0/en/replace.html

Regards,
Bill K.

Re: newbie q: update and/or insert?

am 13.04.2006 21:28:25 von Good Man

"Pierre Gilquin" wrote in
news:443e5a26$1_2@news.bluewin.ch:

> You are looking for an object-relationnal mapping. This can do that
> (and much more). But PHP is a beginner with objects. For now, this can
> be found in Java, C++ languages only.

?????