1

Let's say i have and array like this

$array= Array('id'=>'3', 'name'=>'NAME', 'age'=>'12');

Keys from this array are name of columns in table and values are value of columns which i need to update. I want to update the table based on keys and values. I am using ADODB Please help me

Jerodev
  • 32,252
  • 11
  • 87
  • 108
OsomA
  • 155
  • 1
  • 18

5 Answers5

3

try this:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   $sql .= $key . " = " . $value . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas

and of course the WHERE clause:

$sql .= " WHERE condition = value";

you will get the string:

UPDATE table SET id = 3, name = NAME, age = 12 WHERE condition = value

L.E: You might need to add apostrophes to strings so I have to change my code to something like this:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   if(is_numeric($value))
      $sql .= $key . " = " . $value . ", "; 
   else
      $sql .= $key . " = " . "'" . $value . "'" . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas

$sql .= " WHERE condition = value";

which will produce this:

UPDATE table SET id = 3, name = 'NAME', age = 12 WHERE condition = value

L.E 2: If you want the id column in your condition, the code becomes this:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   if($key == 'id'){
      $sql_condition = " WHERE " . $key . " = " . $value;
      continue;
   }
   if(is_numeric($value))
      $sql .= $key . " = " . $value . ", "; 
   else
      $sql .= $key . " = " . "'" . $value . "'" . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas

$sql .= $sql_condition;

which will produce this result:

UPDATE table SET name = 'NAME', age = 12 WHERE id = 3

Hope this helps! :D

Ares Draguna
  • 1,641
  • 2
  • 16
  • 32
1
foreach ($update_array as $key => $testimonials) {
    $name = mysql_real_escape_string($testimonials->name);
    $content = mysql_real_escape_string($testimonials->content);
    $id = intval($testimonials->id);

    $sql = "UPDATE testimonials SET name='$name', content='$content' WHERE id=$id";
    $result = mysql_query($sql);
    if ($result === FALSE) {
        die(mysql_error());
    }
}

Source : https://stackoverflow.com/a/7884331/3793639

Other sources to check. PHP SQL Update array and Simple UPDATE MySQl table from php array

Community
  • 1
  • 1
Kavvson
  • 825
  • 3
  • 9
  • 23
0

You could use something like this for achieving that:

foreach($values as $value) {
  if(!key_exists($value, $item)) {
    return false;
  }

    $table->{$value} = $items[$value];
}
Matheno
  • 4,112
  • 6
  • 36
  • 53
0

Assuming that the key index is always id and that adodb can use named placeholders you could do this:

$array = Array('id'=>'3', 'name'=>'NAME', 'age'=>'12');
$set = array();
$data = array();
while(list($key,$value)=each($array)) {
    $data[':'.$key] = $value;
    if($key!='id') {
        $set[] = $key . ' = :' . $key;
        // if no placeholders use $set[] = $key . " = '" . database_escape_function($value) . "'";
    }
}
$sql = "UPDATE table SET ".implode($set, ',')." WHERE id=:id";

//$data is now Array(':id'=>'3', ':name'=>'NAME', ':age'=>'12');
//$sql is now  "UPDATE table SET name=:name, age=:age WHERE id=:id";

$stmt = $DB->Prepare($sql);
$stmt = $DB->Execute($stmt, $data);
-2

This is probably the shortest and easiest for you, you can also use something like this to achieve it:

$array = Array('id'=>'3', 'name'=>'NAME', 'age'=>'12');

$sql = "UPDATE table SET ";

$sql .= implode(', ', array_map(function($key, $value){
    return is_numeric($value) ? "{$key} = {$value}" : "{$key} = '". mysql_real_escape_string($value). "'";
}, array_keys($array), $array));

$sql .= " WHERE id = 123";



// Result : UPDATE table SET id = 3, name = 'NAME', age = 12 WHERE id = 123
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46