Jump to content

Simple Foreach statement with mysql update not working


acctman

Recommended Posts

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

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'].

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.