|
Posted by Marcin Dobrucki on 02/23/06 12:51
Lag wrote:
> Having a problem updating my database from a web page, through a
> submission form. Can anyone help?
>
> ----THIS IS MY CODE IN update.php----(user, pass, and database are
> typed in directly, I changed them here for securiy reasons :) )
>
> <?php
>
> /*---CONNECT TO DATABASE---*/
>
> $conn = mysql_connect("localhost", "user", "pass") or
> die(mysql_error());
> mysql_select_db("database",$conn) or die(mysql_error());
>
> $event = $_GET["id"];
> $title=$_POST['title'];
> $date=$_POST['datetime'];
> $desc=$_POST['desc'];
>
> //---UPDATE ENTRY IN DATABASE---
> $query = "UPDATE 'calendar_events' SET 'event_title' = '$title',
> 'event_start' = '$date', 'event_shortdesc' = '$desc' WHERE 'id' =
> $event";
>
> //LIST ITEMS
> echo "$event $title $date $desc";
>
> mysql_query($query);
> echo "Record Updated";
> mysql_close();
>
> ?>
require_once ("DB.php"); // PEAR::DB
$db =& DB::connect(/*db params*/);
$fields = array ("event_title" => $title,
"event_start" => $date,
"event_shortdesc" => $desc);
$res = $db->autoExecute("calendar_events",$fields,
DB_AUTOQUERY_UPDATE,
"id = " . $event);
if (DB::isError($res)) die ($res->getMessage());
echo "Record updated";
With a small change you can also use the autoExecute for inserts with
just about identical code without repeating yourself.
[Back to original message]
|