You are here: Re: Determining whether to INSERT or UPDATE « PHP Programming Language « IT news, forums, messages
Re: Determining whether to INSERT or UPDATE

Posted by Aaron Gray on 01/02/08 01:54

"The Natural Philosopher" <a@b.c> wrote in message
news:1199238401.6726.2@proxy02.news.clara.net...
> Aaron Gray 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 INSERT
>>>> if it
>>>> does not ?
>>>>
>>>> The code I have written does not seem to do the job correctly.
>>>>
>>>> $result = mysql_query( "SELECT page FROM pages WHERE title = '" .
>>>> $title
>>>> . "';");
>>>>
>>>> $row = mysql_fetch_array( $result, MYSQL_ASSOC);
>>>>
>>>> if ( isset( $row["title"]) && $row[title] == $title)
>>>> {
>>>> $result = mysql_query( "UPDATE pages SET page = '". $page . "'
>>>> WHERE
>>>> title = '" .$title . "';");
>>>> if ($result)
>>>> mysql_query( "COMMIT;");
>>>> }
>>>> else
>>>> {
>>>> $result = mysql_query( "INSERT INTO pages SET title ='" .
>>>> $title .
>>>> "', page = '" . $page . "';");
>>>> if ($result)
>>>> mysql_query( "COMMIT;");
>>>> };
>>>>
>>>> This code is buggy and doing an insert rather than an update. How would
>>>> 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.
>>
>> Aaron
>>
>>
> Well you have parentheses round one array index and not another..so start
> there.
> f ( isset( $row["title"]) && $row[title] == $title)

Thats a typo not actual code.

Aaron

 

Navigation:

[Reply to this 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

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