Question

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;
....
Was it helpful?

Solution

This must be

 if( ! empty( $$val )) {

this

 if( ! empty( $val )) {

And inside your foreach change this

$values[] = $$val;

to this

$values[] = $val;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top