3raser Posted February 11, 2012 Share Posted February 11, 2012 I made a small editing system for my news page, and I need to update three columns within my table "announcements" in the database. I tried a method of updating all of them with one MySQL query instead of using three as it just isn't neat. I've searched several methods via google and I've tried all of them, but just can't seem to get it to work. Is this MySQL query correct? mysql_query("UPDATE announcements SET title = {$title} WHERE id = '$id', content = {$content} WHERE id = '$id', lastmodified = ". date('M-d-Y') ." WHERE id = '$id'"); Quote Link to comment https://forums.phpfreaks.com/topic/256874-mysql-query-to-update-multiple-columns-at-once/ Share on other sites More sharing options...
Psycho Posted February 11, 2012 Share Posted February 11, 2012 So, you want to update three fields FOR THE SAME RECORD? You are making it too hard. $query = "UPDATE announcements SET title = {$title}, content = {$content}, lastmodified = NOW() WHERE id = '{$id}'"; mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/256874-mysql-query-to-update-multiple-columns-at-once/#findComment-1316916 Share on other sites More sharing options...
3raser Posted February 11, 2012 Author Share Posted February 11, 2012 So, you want to update three fields FOR THE SAME RECORD? You are making it too hard. $query = "UPDATE announcements SET title = {$title}, content = {$content}, lastmodified = NOW() WHERE id = '{$id}'"; mysql_query($query); Thank you, this is greatly appreciated. And may I ask you why you put now()? Also, how does separating the query into a variable make it more efficient? Quote Link to comment https://forums.phpfreaks.com/topic/256874-mysql-query-to-update-multiple-columns-at-once/#findComment-1316917 Share on other sites More sharing options...
trq Posted February 11, 2012 Share Posted February 11, 2012 Thank you, this is greatly appreciated. And may I ask you why you put now()? It does the same thing as calling php's date() as you did but uses a mysql function instead. Also, how does separating the query into a variable make it more efficient? It doesn't. But it is a good habit to get into as it can come in handy when debugging. Quote Link to comment https://forums.phpfreaks.com/topic/256874-mysql-query-to-update-multiple-columns-at-once/#findComment-1316919 Share on other sites More sharing options...
3raser Posted February 11, 2012 Author Share Posted February 11, 2012 Thank you, this is greatly appreciated. And may I ask you why you put now()? It does the same thing as calling php's date() as you did but uses a mysql function instead. Also, how does separating the query into a variable make it more efficient? It doesn't. But it is a good habit to get into as it can come in handy when debugging. Thank you for the information. Quote Link to comment https://forums.phpfreaks.com/topic/256874-mysql-query-to-update-multiple-columns-at-once/#findComment-1316921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.