tags:

views:

45

answers:

1

Hi im trying to build a foreach of sort of , that would extract($_POST) then utf8_decode each post so i dont have to do this manually for each $ variable, is it easy to make such function, im tired..thanks for any help.

+2  A: 

Instead of writing every variable into query by hand, you can just name the field names, by using this simple function to produce SET statement:

function dbSet($fields) {
  $set='';
  foreach ($fields as $field) {
    if (isset($_POST[$field])) {
      $set.="`$field`='".mysql_real_escape_string($_POST[$field])."', ";
    }
  }
  return substr($set, 0, -2); 
}

and usage as simple as:

$fields = explode(" ","name surname lastname address zip fax phone");
$query  = "UPDATE $table SET ".dbSet($fields)." WHERE id=$id";

Neat, eh?

Col. Shrapnel
yep looks cool , by the way im pretty new to php, what the substr($set, 0, -2); is for?
tada
@tada: it's removes the last ", " at the end of the $set variable.
Cesar
Nice, but this still needs `utf8_decode()` ing or `iconv()` as requested by the OP ;)
Pekka