PHP - Update MySQL table with data from array
Posted: 06 Apr 2015, 14:04
Hello,
I have the following method to update data in MySQL table.
I use this code to call the update() method:
When I apply: var_dump($sql); i get this result:
How to change the method to add all data from the array $rows in the $sql string, so to result this SQL query?
Thanks.
I have the following method to update data in MySQL table.
Code: Select all
public function update($table, $rows){
foreach($rows as $k => $v) {
$sql = "UPDATE $table SET $k='$v'";
}
$this->query($sql);
}
I use this code to call the update() method:
Code: Select all
require_once 'db.php';
$db = new db;
$db->update('admin', ['password'=>'test','email'=>'test']);
Code: Select all
$sql = "UPDATE admin SET email='test'";
Code: Select all
$sql = "UPDATE admin SET password='test', email='test'";