[prog] mysql insert/update/replace

Michelle Murrain michelle at murrain.net
Mon Jul 25 12:55:14 EST 2005


On Jul 24, 2005, at 9:12 PM, Wolf Rising wrote:

> Hi,
>
> I'm trying to have a form automatically update(overwrite) a previous
> entry if needed, I have a select statement to check if any entries
> already exists:
>
> select * from status where date = now();
>
> if this returns nothing I do an insert:
>
> $query = "insert into status values ('1', now(), '$one'), ('2', now(),
> '$two'), ('3', now(),'$three'), ('4', now(),'$four');";
>
> there are four rows each time that need to be effected. If the
> entries already exist I want to be able to update/replace the
> current entries with new ones. I had found on duplicate key but
> it doesn't work with the older version of mysql that is installed.
>
> Is it possible using either update or replace to effect all
> four rows the way it's possible to insert the information for
> all four rows at once?


Well, you need some sort of update query, that selects the four rows,  
like:

update status set foo = "$bar", fee = "$fum" where date = "$date"

Is that the sort of thing you are looking for?

It's hard to write something more specific - because I'm not sure  
what your original query is supposed to do - it seems weird to create  
4 records at once, but that could just be your specific application.  
(I'd try to make it one record, and increase the number of fields in  
the table, or make related tables, but I don't know enough about what  
you're trying to do.)

Michelle

Michelle Murrain
Seminarian, Pacific School of Religion
michelle at murrain.net
AIM:pearlbear0
Blog:www.pearlbear.us


"Love the earth and the sun and the animals, despise riches, give  
alms to everyone that asks, stand up for the stupid and crazy, devote  
your income and labor to others, hate tyrants, argue not concerning  
God."  --Walt Whitman






More information about the Programming mailing list