Re: problem in updating data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>"Sukanto Kho" <starofflame@xxxxxxxxxxx> wrote in message
news:BAY2-DAV9KFRJrzdDW60001cc03@xxxxxxxxxxxxxx
>Hi All,
>
>I've created an function for update record in database's table.
>
>the function can handle all the record update from any table... with
parameter passed in .. like this :
>
>function update($table,$values,$condition)
>   { $link=$this->database();
>     $fields = mysql_list_fields("wellindo", $table, $link);
>              $columns = mysql_num_fields($fields);
>              $field_values="";
>     for ($i = 1; $i < $columns-1; $i++) {
>                 $field_values=$field_values.mysql_field_name($fields,
$i)."=".$values[$i].",";}
>     $field_values=$field_values.mysql_field_name($fields,
$i)."=".$values[$i];
>     $query = "UPDATE $table SET $field_values $condition";
>    $this->query($query,$table);
>     }
>the function will find all the fields of the table then update all the
data.
>
>the problem is that when there ar some fields that I dont want to update (I
still want to use the existed/old data )...
>so how do I do that...??
>
>eg : I hav record
>name   phone      country
>a          123        England
>and i want to update only phone change to 234.... what can do to set name
and country so that it still remain the old >data

Hi Sukanto,

you could change your values array to an associative array. This way you
don't need to get the field names from the table every time:

$values = array('field1' => 'myValue', 'field2' = 25);

Then you build your update statement by looping through this array and use
the array keys as the field names.

$field_values = '';

foreach ($values as $column => $value) {
    $field_values .= (empty($field_values)) ? "SET $column = '$value'" : ",
SET $column = '$value'";
}

Hope this helps,

Torsten Roehr

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux