Reply to Re: Determining whether to INSERT or UPDATE

Your name:

Reply:


Posted by Rik Wasmus on 01/02/08 01:07

On Wed, 02 Jan 2008 02:04:14 +0100, Aaron Gray <ang.usenet@gmail.com> =

wrote:

> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:y_WdnRKnboihf-fanZ2dnUVZ_sPinZ2d@comcast.com...
>> Aaron Gray wrote:
>>> I have a MySQL table of pages each with a 'title' and 'page' fields.=

>>>
>>> How do I do either an UPDATE if the pages 'title' exists or an INSER=
T =

>>> if
>>> it
>>> does not ?
>>>
>>> The code I have written does not seem to do the job correctly.
>>>
>>> $result =3D mysql_query( "SELECT page FROM pages WHERE title =3D=
'" .
>>> $title
>>> . "';");
>>>
>>> $row =3D mysql_fetch_array( $result, MYSQL_ASSOC);
>>>
>>> if ( isset( $row["title"]) && $row[title] =3D=3D $title)
>>> {
>>> $result =3D mysql_query( "UPDATE pages SET page =3D '". $pag=
e . "'
>>> WHERE
>>> title =3D '" .$title . "';");
>>> if ($result)
>>> mysql_query( "COMMIT;");
>>> }
>>> else
>>> {
>>> $result =3D mysql_query( "INSERT INTO pages SET title =3D'" =
.. =

>>> $title
>>> .
>>> "', page =3D '" . $page . "';");
>>> if ($result)
>>> mysql_query( "COMMIT;");
>>> };
>>>
>>> This code is buggy and doing an insert rather than an update. How wo=
uld
>>> you approach this task ?
>>>
>>> Many thanks in advance,
>>>
>>> Aaron
>>>
>>>
>>>
>>>
>>
>> Try comp.databases.mysql. This is a PHP newsgroup.
>
> The problems I am having seem to be the PHP side of things not the SQL=
=

> side.

The MySQL side could handle this far better then the PHP side though, =

certainly when there could be concurrent updates/inserts.



-- =

Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация