Sorry in advance for my amateur knowledge. I'm creating the basics of a user a database, I have registering working fine fine but I cannot get my update function working correctly.
Register function
mysql_query("INSERT INTO `users` ($fields) VALUES ($data)");
where $fields and $data are taken from a post filled array:
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';
Update function
However using this method for updating records does not work. I have tried 2 methods:
mysql_query("UPDATE `users` SET ($fields) VALUES ($data) WHERE `user_id` = $user_id");
and
mysql_query("UPDATE `users` SET $fields = $data WHERE `user_id` = $user_id");
The first SQL function does not work (annoying as it does of INSERT) and the second one only updates one value from my array.
Can someone correct me on how this should be done? For info my array is as follows:
$save_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'email' => $_POST['email'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'bio' => $_POST['bio']
);
mysql_queryinterface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs.echo "UPDATEusers` SET ($fields) VALUES ($data) WHEREuser_id= $user_id";` beforemysql_query(...just to check what your trying to query first and debug. I think the problem could be email @ character