so i'm using a foreach loop to loop through a form i've send with javascript. In this loop I use the keys(input fields names) and values(input values) and use them to build my query, I still check for nulled values and other field types and then i add them to a variable . I'm wonderin if this is really the best idea if I value the security of my application. Could someone give me some insight? this is a piece of code to better describe what I mean:
$isValid = true;
$fouteVelden = array();
$verplichtArray = array('naam','categorie','prijs_inkoop');
foreach($_POST['gegevens'] as $key => $value){
if(in_array($key,$verplichtArray)){
if($value == ""){
$isValid = false;
$fouteVelden[] = $key;
$message = "Een of meerdere van de velden waren leeg.";
}
}
}
if($isValid == true){
foreach($_POST['gegevens'] as $key => $value){
if($key == "categorie"){
if($value == "kies"){
$isValid = false;
$fouteVelden[] = $key;
$message = "Kies eerst een categorie.";
}
}
}
}
if($isValid == true){
foreach($_POST['gegevens'] as $key => $value){
if($key == "prijs_inkoop"){
$value = str_replace(',','.',$value);
if(!is_numeric($value)){
$isValid = false;
$fouteVelden[] = $key;
$message = "Er zijn alleen getallen mogelijk bij prijs inkoop.";
}
}
}
}
if($isValid == true){
$branche = new Branches;
$branche->getBranche('id',$_SESSION['branche']);
$conn = new Connection2($branche->database,$branche->dbuser,$branche->dbpass);
$notneeded = array("subcatvan");
$nulledout = array("besteleenheid");
foreach($_POST['gegevens'] as $key => $value){
if(!in_array($key,$notneeded)){
if($key == "prijs_inkoop" || $key == 'prijs_verkoop'){
$value = str_replace(',','.',$value);
$fields .= "`".$conn->real_escape_string($key)."`,";
$values .= "'".$conn->real_escape_string($value)."',";
}else{
if(in_array($key,$nulledout)){
if($value != ""){
$value = str_replace(',','.',$value);
if(!is_numeric($value)){
$isValid = false;
$fouteVelden[] = $key;
$message = "Er zijn alleen getallen mogelijk bij ".$key.".";
}else{
$fields .= "`".$conn->real_escape_string($key)."`,";
$values .= "'".$conn->real_escape_string($value)."',";
}
}else{
$fields .= "`".$conn->real_escape_string($key)."`,";
$values .= "'0',";
}
}else {
$fields .= "`".$conn->real_escape_string($key)."`,";
$values .= "'".$conn->real_escape_string($value)."',";
}
}
}
}
$fields .= "`merk`,`thema`,`visible`,`volgnr`,`gewijzigd`";
$values .= "'0','0','Y','0',now()";
if($isValid == true){
$conn->query("INSERT INTO `product` (".$fields.") VALUES (".$values.")");
$message = $conn->inserted_id();
}
}
if($isValid == false){
$dataArray = array(
"isValid" => $isValid,
"message" => $message,
"fouteVelden" => $fouteVelden
);
echo json_encode(utf8json($dataArray));
}
if($isValid == true){
$dataArray = array(
"isValid" => $isValid,
"message" => $message,
);
echo json_encode(utf8json($dataArray));
}
$fouteVelden[]is, I can guess but I could be wrong. I don't know what the$messagevariable is set to... the list goes on. please translate before you post, or even better, use English when coding. Not sure what the standard is on this. \$\endgroup\$