Re: problem in updating data
| From: | Torsten Roehr | Date: | Mon, 28 Jun 2004 11:27:24 +0000 |
| Subject: | Re: problem in updating data | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-35016@lists.php.net to get a copy of this message | ||
>"Sukanto Kho" <starofflame@hotmail.com> wrote in message
news:BAY2-DAV9KFRJrzdDW60001cc03@hotmail.com...
>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