I have an html form in which I want to use the inputs in a SQL query. Instead of the first php code, I want to use the alternative php code below, but I receive php parse error for the line including ${$key} = $value
in alternative php code. maybe this syntax has been removed in newer versions of php. I appreciate if anyone could help me correct the syntax or suggest a different code to do me the same thing. Thanks in advance.
The error I receive: Parse error: "syntax error, unexpected '$', expecting case (T_CASE) or default (T_DEFAULT) or '}' "
html code:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<select name="my_input1">
<option value="val1">a</option>
<option value="val2">b</option>
<option value="val3">c</option>
</select>
<select name="my_input2">
<option value="val4">d</option>
<option value="val5">e</option>
</select>
</form>
main php code:
$inputs = array();
$queries = array();
foreach($inputs as $key => $value){
array_push($queries,"table_name.column1 = $_POST['my_input1']");
array_push($queries,"table_name.column2 = $_POST['my_input2']");
}
Alternative php code:
$inputs = array();
$queries = array();
foreach($inputs as $key => $value){
${$key} = $value;
array_push($queries,"table_name.column1 = $my_input1");
array_push($queries,"table_name.column2 = $my_input2");
}