Hey been at this for a day can't fix the problem. This function updates user info in the db [MySQL] it keeps throwing the exception Couldn't update user
<?php
function update_user($user_id, $update_data) {
global $db;
$update = array();//array_walk($update_data, 'array_sanitize');
foreach ($update_data as $field=>$data) {
if ($field != 'allow_email') {
$update[] = '`' . $field . '` = \'' . $data . '\'';
} else {
$update[] = '`' . $field . '` = ' . $data;
}
}
$updateImploded = implode(', ', $update);
var_dump($updateImploded);
$stmt = $db->prepare("UPDATE `users` SET :updateImploded WHERE `user_id` = :user_id");
try {
$success = $stmt->execute(array(':updateImploded'=>$updateImploded, ':user_id'=> $user_id));
var_dump($stmt);
} catch (PDOException $e) {
die ('Couldn\'t update user');
}
//mysql_query("UPDATE `users` SET" . implode(', ', $update) . " WHERE `user_id` = '$user_id'");
}
EDIT 2 After revising it to use php to prepare the statement
foreach ($update_data as $field=>$data) {
$update[] = '`' . $field . '` = \'' . $data . '\'';
}
$updateImploded = 'UPDATE `users` SET' . implode(', ', $update) . 'WHERE `user_id` =' .$user_id;
$stmt = $db->prepare($updateImploded);
try {
$success = $stmt->execute();
var_dump($stmt);
} catch (PDOException $e) {
die ('Cant update user');
}
}
$e->getMessage()
to yourdie
message. – Sam Selikoff May 10 '13 at 3:19first_name
= \'Saeeb\',last_name
= \'Msarwa\',email
= \'[email protected]' at line 1 – Saeb Msarwa May 10 '13 at 3:43'
around them, not`
. Also, using implode like this is not really in the spirit of named parameters. It may seem to make the implementation easier, but it's less maintainable and more subject to errors in the future. Try to get it working by hard-coding the field names in, and using a separate:var
for each value. – Sam Selikoff May 10 '13 at 5:33