This must be
if( ! empty( $$val )) {
this
if( ! empty( $val )) {
And inside your foreach change this
$values[] = $$val;
to this
$values[] = $val;
Pergunta
Can someone please tell me why this code is not working. I browsed through previous answers and the replies were because the array is not set but here it is set?
$cars = array('Volvo','BMW','Toyota');
foreach($cars as $val) {
if( ! empty( $$val )) {
$felter[] = $val;
$values[] = $$val;
}
}
$feltstr = implode(',' , $felter);
$valuestr = "'" . implode("', '", $values) . "'";
$sql= "INSERT INTO my-cars (id, $feltstr) VALUES ('', $valuestr)";
echo $sql;
....
Solução
This must be
if( ! empty( $$val )) {
this
if( ! empty( $val )) {
And inside your foreach change this
$values[] = $$val;
to this
$values[] = $val;