acctman Posted October 19, 2008 Share Posted October 19, 2008 This doesn't work although it looks correct something is stopping it for updating... I've echo'ed all the $col $value its all correct when it prints out so the information is being retreived. When the foreach statement is run only the first 3 updates are month, day and year... foreach ($_POST[add] as $col => $value) { $_POST[add][$col] = strip_tags(stripslashes($value)); $entry = sql_escape_string(stripslashes($value)); echo "m_".$col; echo $entry . "<br>"; mysql_query("UPDATE rate_members SET m_$col = $entry WHERE m_id=$m_id"); } this works but as you can see its definitely not the correct way to do it not to mention resources that would used. mysql_query("UPDATE rate_members SET m_day='".sql_escape_string(stripslashes($_POST[add][day]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_month='".sql_escape_string(stripslashes($_POST[add][month]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_year='".sql_escape_string(stripslashes($_POST[add][year]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_state='".sql_escape_string(stripslashes($_POST[add][state]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_city='".sql_escape_string(stripslashes($_POST[add][city]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_zip='".sql_escape_string(stripslashes($_POST[add][zip]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_status='".sql_escape_string(stripslashes($_POST[add][status]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_orientation='".sql_escape_string(stripslashes($_POST[add][orientation]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_ethnicity='".sql_escape_string(stripslashes($_POST[add][ethnicity]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_turnon='".sql_escape_string(stripslashes($_POST[add][turnon]))."' WHERE m_id='$m_id'"); mysql_query("UPDATE rate_members SET m_turnoff='".sql_escape_string(stripslashes($_POST[add][turnoff]))."' WHERE m_id='$m_id'"); Link to comment https://forums.phpfreaks.com/topic/129042-simple-foreach-statement-with-mysql-update-not-working/ Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 Why not do it all in the one query? You can update multiple fields with one query. UPDATE table SET field1='somethingelse', field2='somethingelse', field3='somethingelseagain' WHERE something='eh' Link to comment https://forums.phpfreaks.com/topic/129042-simple-foreach-statement-with-mysql-update-not-working/#findComment-668958 Share on other sites More sharing options...
corbin Posted October 19, 2008 Share Posted October 19, 2008 Feeling picky, so I'll point this out.... Anytime a string isn't quoted in PHP (I really shouldn't say anytime, since that's not true, but in most contexts), it is assumed to be a constant. If the constant doesn't exist, then it is used as a literal string. Hence: array[key] is wrong unless key is an actual constant, which in this case, I doubt it is. So, you should be using array['key']. Link to comment https://forums.phpfreaks.com/topic/129042-simple-foreach-statement-with-mysql-update-not-working/#findComment-668964 Share on other sites More sharing options...
acctman Posted October 19, 2008 Author Share Posted October 19, 2008 thanks, i'm going to try both fixes from projectfear and corbin and see what happens Link to comment https://forums.phpfreaks.com/topic/129042-simple-foreach-statement-with-mysql-update-not-working/#findComment-668967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.