My code below will not insert into my database. I do not know where my misstake is being made. (Thanks for the notifications regarding sql injections, will read about that laters <3)
This is my php code so far:
$sqlArray = array();
$nameArray = array();
$valueArray = array();
foreach($_POST as $name => $value) {
//$sqlArray[] = "':".$name."'=>$".$name;
$nameArray[] = $name;
$valueArray[] = "'".$value."'";
}
$names = implode(', ', $nameArray);
$values = implode(', ', $valueArray);
$sql = "INSERT INTO random ( ".$names." ) VALUES ( ".$values." )";
$addRandom = $dbh->prepare( $sql );
$addRandom->execute();
And the output by $sql looks like:
INSERT INTO random ( random1, random2, zipCode) VALUES ( 'Namn', 'Adress', 'Zipcode' )
What should I change or add?
See Question&Answers more detail:os