thats good.
in my current project i think i will be creating a dynamic sql query with the names and variables so i dont want:
INSERT INTO table (name, age, submit) VALUES ('dave', '39', 'Submit!')
🙂
so, if ive learnt how to use foreach correctly... would this work for creating a dynamic sql query?
$sql = "INSERT INTO table (";
foreach($_POST as $key => $value) {
$sql .= "$key, ";
}
$lastcomma = strrpos($sql,",");
$lastcomma--;//get position of char before last ,
$sql = substr($sql,0,$lastcomma);
$sql .= ") VALUES (";
foreach($_POST as $key => $value) {
$sql .= "'$value',";
}
$lastcomma = strrpos($sql,",");
$lastcomma--;//get position of char before last ,
$sql = substr($sql,0,$lastcomma);
$sql .= ")";
echo $sql;
in the first foreach loop is there a way just to get the $keys and not the $values aswell?