i have a dynamically field that have a maximum of 4 inputs.
If i write 4 inputs that exists in the database i get
input0 company // $num0
"error" = $num1
input1 company // $num2
"error" = $num3
input2 company // $num4
"error" = $num5
input3 company // $num6
But if not exists in the database the order of first input is changed because the while is not played:
"error" = $num0
input1 company // $num1
and the order is changed....//problem
the main problem is that i need to know what is the input that exists in the database or not.
script
function checkEmployeer($db, $form) {
$i=0;
foreach ($form['job'] as $value) {
$sql = $db -> prepare("SELECT `employer` FROM `employer` WHERE employer=?");
$sql -> bind_param('s', $value);
$sql -> execute();
while ($sql -> fetch()) { //problem
${'num'.$i++} = $value; //problem - $num0 = $value if exists in DB
}
${'num'.$i++} = "error"; //problem - $num0 or $num1 = depends of while result...
}
for ($i = 0; $i < sizeof($form['job']); $i++) {
if (${'num'.$i} != "error" && ${'num'.$i} == $form['job'][$i]) {
echo ("already exists in the db".$i);
}
else {
$sql = $db -> prepare("INSERT INTO `database_charts`.`employer` (`employer`) VALUES (?)");
$sql -> bind_param('s', $form['job'][$i]);
$sql->execute();
echo ("insert");
}
}
}
how i can solve that? i don’t have any idea…
Create a key for employer and INSERT it without checking anything, affected_rows() will tell you if it has been inserted or not.
You will not get duplicates if
database_charts.employeris defined as key.