Home > Archive > PHP DB > June 2004 > problem in updating data
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem in updating data
|
|
| Sukanto Kho 2004-06-28, 8:56 am |
| 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
sorry for the awful explaination....hope someone can get it...
thanx
By regard;
Sukanto
| |
| Torsten Roehr 2004-06-28, 8:56 am |
| >"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
|
|
|
|
|