acctman Posted October 16, 2008 Share Posted October 16, 2008 Am I missing something in the Foreach statement that is causing the query not to update? I did a standalone test and the query updated, but when I run the code below nothing happens. foreach ($_POST[add] as $col => $value) $_POST[add][$col] = strip_tags(stripslashes($value)); { mysql_query("UPDATE rate_members SET m_'".$col."'='".sql_escape_string(stripslashes($value))."' WHERE m_id='$m_id'"); } standalone test to make sure the database updates. works fine mysql_query("UPDATE rate_members SET m_year='".sql_escape_string(stripslashes($_POST[add][year]))."' WHERE m_id='$m_id'"); Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/ Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Looks like you are making your foreach loop execute only the first statement. Move your opening { back one line: foreach ($_POST[add] as $col => $value) { $_POST[add][$col] = strip_tags(stripslashes($value)); mysql_query("UPDATE rate_members SET m_'".$col."'='".sql_escape_string(stripslashes($value))."' WHERE m_id='$m_id'"); } Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/#findComment-667591 Share on other sites More sharing options...
acctman Posted October 17, 2008 Author Share Posted October 17, 2008 Looks like you are making your foreach loop execute only the first statement. Move your opening { back one line: foreach ($_POST[add] as $col => $value) { $_POST[add][$col] = strip_tags(stripslashes($value)); mysql_query("UPDATE rate_members SET m_'".$col."'='".sql_escape_string(stripslashes($value))."' WHERE m_id='$m_id'"); } still not working. I removed everything from the processing php file so its only the foreach & query. the standalone version works fine, but not this coding. I also removed everything from the FORM i just have a month date and year select boxes. so its very basic for the testing yet still not Updating the changes Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/#findComment-667595 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 hmm okay well how about putting your query string into a variable and echoing it out to see if it's what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/#findComment-667604 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 I suspect it's going to look something along the lines of UPDATE rate_members SET m_'year'='.... Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/#findComment-667605 Share on other sites More sharing options...
acctman Posted October 17, 2008 Author Share Posted October 17, 2008 ok i figured it out with this code. setting the value into a var did the trick and I removed the quotes from around the $col var foreach ($_POST[add] as $col => $value) { $_POST[add][$col] = strip_tags(stripslashes($value)); // mysql_query("UPDATE rate_members SET m_'".$col."'='".sql_escape_string(stripslashes($value))."' WHERE m_id='$m_id'"); echo $col; $entry = sql_escape_string(stripslashes($value)); echo $entry; mysql_query("UPDATE rate_members SET m_$col = $entry WHERE m_id=$m_id"); } Quote Link to comment https://forums.phpfreaks.com/topic/128779-foreach-on-form-sql-query-update-not-working/#findComment-667637 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.