I have a dynamic form that uses variable variables and it works well, but I came to an issue where some POST values must be removed once they are processed by the form's submission.
Due to the nature of variable variables and this dynamic form's programming that builds INSERT and UPDATE queries dynamically, I need to remove these values from the $_POST
once they've been processed into a single variable.
The partial form processing section is in a foreach loop and contains:
$Fields = array();
$Values = array();
foreach ($_POST as $key=>$value ) :
if (Contains("month", $key)) unset($_POST);
if (Contains("day", $key)) unset($_POST);
if (Contains("year", $key)) unset($_POST);
if (Contains("hour", $key)) unset($_POST);
if (Contains("minute", $key)) unset($_POST);
if (Contains("second", $key)) unset($_POST);
$Fields[] = "`$key`";
$Values[] = isNull($value, $DBName);
endforeach;
$sqlInsert = "INSERT INTO $TableName (".implode(",",$Fields).") VALUES (".implode(",",$Values).")";
The Contains()
function has:
function Contains($searchWord, $fromString) {
if (is_array($fromString)) :
reset($fromString);
$key = key($fromString);
return strpos($key, $searchWord) !== FALSE;
else:
return strpos($fromString, $searchWord) !== FALSE;
endif;
}
I tried everything I can think of including:
foreach ($_POST as $key=>$value ) :
if (Contains("month", $key)) unset($_POST[$key]);
if (Contains("day", $key)) unset($_POST[$key]);
if (Contains("year", $key)) unset($_POST[$key]);
if (Contains("hour", $key)) unset($_POST[$key]);
if (Contains("minute", $key)) unset($_POST[$key]);
if (Contains("second", $key)) unset($_POST[$key]);
endforeach;
as $key
should contain the name of the POST field that I am trying to unset()
but they do not unset. Any ideas?
No sooner did I rewrite and simplify my original question when doing so gave me an idea that seems to have done the trick. Rather than using unset, I used continue:
if (Contains("month", $key)) continue;
if (Contains("day", $key)) continue;
if (Contains("year", $key)) continue;
if (Contains("hour", $key)) continue;
if (Contains("minute", $key)) continue;
if (Contains("second", $key)) continue;