I have a function that receives a an array from the $_POST function then uses the indexes and values contained in the indexes to create an SQL. My problem is that I can get the function to echo the SQL correctly but I'm unable to create a variable. My function is below
function createcontactsArray($sql,Array $contactsArray){
//array has already been cleaned from sql injections
//delete null variables and the value of the submit button
foreach ($contactsArray as $key => $value) {
if($value == ""||$value=="continue") {
unset($contactsArray[$key]);
}
}
echo "INSERT INTO users(";
//create list of tables to use in the database
foreach ($contactsArray as $key => $value) {
if ($value == end($contactsArray)) {
echo $key;
} else {
echo $key.",";
}
}
echo ') VALUES (';
//create list of tables to use in the database
//$newcontactsArray = array_values($contactsArray);
foreach ($contactsArray as $key => $value) {
if ($value == end($contactsArray)) {
echo '"'.$value.'"';
} else {
echo '"'.$value.'"'.",";
}
}
echo ');';
}
If you run this script and pass it an associative array for example $contacts = array("name"=>"Peter griffin","town"=>"Quahogn");it will output the following INSERT INTO users (name,contacts) VALUES ("Peter griffin","Quahog"). However I want the function to create an sql like $sql = INSERT INTO users (name,contacts) VALUES ("Peter griffin","Quahog") so that to output I just say echo $sql; Thanks.