Jump to content

Foreach on FORM sql query update, not working


acctman

Recommended Posts

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'");

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'");
}

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

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");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.