torsuntsu Posted October 26, 2013 Share Posted October 26, 2013 Hi, i´m using this code to update a database, but i get an error, and i can´t figure it out where the problem is. Here´s the code: function alter_page($page_data, $page_id){ array_walk($page_data,'array_sanitize'); $fields= '`'.implode('`, `',array_keys($page_data)).'`'; $data= '\''.implode('\', \'',$page_data).'\''; mysql_query("UPDATE `paginas` SET ($fields)=($data) WHERE `id`= $page_id")or die(mysql_error()); } Need help. Thanks, TorSunTsu Quote Link to comment https://forums.phpfreaks.com/topic/283317-sql-update-with-array-imploded/ Share on other sites More sharing options...
Barand Posted October 26, 2013 Share Posted October 26, 2013 The UPDATE syntax is UPDATE table SET col1 = val1, col2 = val2, .... Quote Link to comment https://forums.phpfreaks.com/topic/283317-sql-update-with-array-imploded/#findComment-1455570 Share on other sites More sharing options...
Psycho Posted October 26, 2013 Share Posted October 26, 2013 function alter_page($page_data, $page_id) { array_walk($page_data, 'array_sanitize'); $setParamsAry = array(); foreach($page_data as $field => $value) { $setParamsAry[] = "`{$field}` = '{$value}'"; } $setParamsStr = implode(', ', $setParamsAry); $query = "UPDATE `paginas` SET {$setParamsStr} WHERE `id`= $page_id"; mysql_query($query)or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/283317-sql-update-with-array-imploded/#findComment-1455574 Share on other sites More sharing options...
Solution torsuntsu Posted October 26, 2013 Author Solution Share Posted October 26, 2013 Thanks Psycho. Problem solved. You Rock!!!! Torsuntsu. Quote Link to comment https://forums.phpfreaks.com/topic/283317-sql-update-with-array-imploded/#findComment-1455575 Share on other sites More sharing options...
Psycho Posted October 26, 2013 Share Posted October 26, 2013 LOL, you selected your response as the best answer? Quote Link to comment https://forums.phpfreaks.com/topic/283317-sql-update-with-array-imploded/#findComment-1455576 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.