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

2

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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];
}

Comments

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);

1 Comment

-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

5 Comments

You shouldn't post such a code, there is already enough SQL injections in the world
@YourCommonSense you can use mysql_real_escape_string as i update my code.
Nope, I can not
@Luuk Yes i already add mysql_real_escape_string for string value, and numeric value don't need mysql_real_escape_string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.