Reply to Re: QUERY AND UPDATE PROBLEM

Your name:

Reply:


Posted by David Haynes on 02/27/06 05:14

Lag wrote:
> <?php
>
> /*---CONNECT TO DATABASE---*/
>
> $conn = mysql_connect("localhost", "******", "******") or
> die(mysql_error());
> mysql_select_db("******",$conn) or die(mysql_error());
>
> $event = $_GET["id"];
> $title=$_POST['title'];
> $date=$_POST['datetime'];
> $desc=$_POST['desc'];
>
> //---UPDATE ENTRY IN DATABASE---
> $result = mysql_query("SELECT * FROM calendar_events WHERE
> id='.$event'",$conn);
> $count = mysql_num_rows($result);
> $query = "UPDATE calendar_events SET event_title = '" . $title . "',
> event_start = '" . $date . "', event_shortdesc = '".$desc."' WHERE id =
> $event";
> mysql_query($query) or die(mysql_error());
>
> //LIST ITEMS
> echo "$event $title $date $desc";
>
> echo "<br>Record Updated! Rows Affected: $count";
> mysql_close();
>
> ?>
>
Why the double query? Why not:

//---UPDATE ENTRY IN DATABASE---
$query = "UPDATE calendar_events SET event_title = '$title',
event_start = '$date', event_shortdesc = '$desc' WHERE id = $event";
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);
....

or, to make it more readable/maintainable:
//---UPDATE ENTRY IN DATABASE---
$query = <<<SQL
UPDATE calendar_events
SET
event_title = '$title',
event_start = '$date',
event_shortdesc = '$desc'
WHERE id = $event
SQL;
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);

or if you don't like the use of <<<
//---UPDATE ENTRY IN DATABASE---
$sql = "UPDATE "
."calendar_events "
."SET "
."event_title = '$title', "
."event_start = '$date', "
."event_shortdesc = '$desc' "
."WHERE id = $event ";
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);

Either of these last two methods allows you to add/subtract/edit the SQL
without having to walk a huge line of code.

-david-

[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

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