I have the following code to process my PHP form:
$result1 = $db->query("SELECT `name` FROM staff_fields WHERE `".$_SESSION['logged_business']."` = 'Yes'")->fetchAll(PDO::FETCH_ASSOC);
$query = "UPDATE staff SET ";
foreach($result1 as $q){
$query .= $q['name']."=:".$q['name'].", ";
}
$query = rtrim($query, ' ,')." WHERE id=:id";
$stmt = $db->prepare($query);
foreach($result1 as $col){
$stmt->bindParam(':'.$col['name'], $_POST[$col['name']]);
}
$stmt->bindParam(':id', $_POST['id']);
if($stmt->execute())
header('Location: staff.php?updated');
else
echo "<center><h1>Error Updating Staff</h1></center>";
And the following code for the form it self.
$stmt = $db->prepare("SELECT * FROM staff_fields WHERE ".$_SESSION['logged_business']."='Yes'");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt1 = $db->prepare("SELECT * FROM staff WHERE id=:id");
$stmt1->bindParam(':id', $_GET['id']);
$stmt1->execute();
while($row1 = $stmt1->fetchAll(PDO::FETCH_ASSOC)){
$table = "<form action='./edit_staff.php' method='post'>
<table align='center'>";
foreach($result as $res){
$table .=
"
<tr>
<td>".$res['label']."</td>
<td>";
if($res['type'] == "text"){
$table .= "<input type='text' name='".$res['name']."' value='".$row1[0][$res['name']]."'/>";
}
if($res['type'] == "date"){
$table .= "<input type='date' name='".$res['name']."' value='";
$date = new DateTime($row1[0][$res['name']]);
$table .= $date->format('Y-m-d')."'/>";
}
if($res['type'] == "yesno"){
$table .= "<select name='".$res['name']."' />";
if($row1[0][$res['name']] == "Yes"){
$table .= "<option value='Yes' selected>Yes</option>
<option value='No'>No</option>";
}else{
$table .= "<option value='No' selected>No</option>
<option value='Yes'>Yes</option>";
}
$table .="</select>";
}
$table .= "</td>
</tr>
";
}
$table .="<tr><td><input type='hidden' number='id' value='".$row1[0]['id']."' /></td><td><input type='submit' name='btn' value='Add Staff' /></td></tr></table></form>";
echo $table;
}
However despite debugging the code and investigating it isn't working. It is the first part of the code that isn't working (form submission). I tried using debug params on the update statement but this is what I get:
SQL: [642] UPDATE staff SET first_name1459057776924=:first_name1459057776924, last_name1459057788088=:last_name1459057788088, job_title1459057796608=:job_title1459057796608, proof_of_age_on_file1459057805910=:proof_of_age_on_file1459057805910, date_of_birth1459057814082=:date_of_birth1459057814082, start_date1459057824504=:start_date1459057824504, signed_contract_in_place1459057835607=:signed_contract_in_place1459057835607, references_received1459057869650=:references_received1459057869650, payroll_informa Params: 11 Key: Name: [24] :first_name1459057776924 paramno=-1 name=[24] ":first_name1459057776924" is_param=1 param_type=2 Key: Name: [23] :last_name1459057788088 paramno=-1 name=[23] ":last_name1459057788088" is_param=1 param_type=2 Key: Name: [23] :job_title1459057796608 paramno=-1 name=[23] ":job_title1459057796608" is_param=1 param_type=2 Key: Name: [34] :proof_of_age_on_file1459057805910 paramno=-1 name=[34] ":proof_of_age_on_file1459057805910" is_param=1 param_type=2 Key: Name: [27] :date_of_birth1459057814082 paramno=-1 name=[27] ":date_of_birth1459057814082" is_param=1 param_type=2 Key: Name: [24] :start_date1459057824504 paramno=-1 name=[24] ":start_date1459057824504" is_param=1 param_type=2 Key: Name: [38] :signed_contract_in_place1459057835607 paramno=-1 name=[38] ":signed_contract_in_place1459057835607" is_param=1 param_type=2 Key: Name: [33] :references_received1459057869650 paramno=-1 name=[33] ":references_received1459057869650" is_param=1 param_type=2 Key: Name: [45] :payroll_information_given_to_ho1459057881692 paramno=-1 name=[45] ":payroll_information_given_to_ho1459057881692" is_param=1 param_type=2 Key: Name: [26] :leaving_date1459057889857 paramno=-1 name=[26] ":leaving_date1459057889857" is_param=1 param_type=2 Key: Name: [3] :id paramno=-1 name=[3] ":id" is_param=1 param_type=2
How come this code isn't working? The staff_fields contains the fields and that is used in the form as well so I don't get why this isn't working.
I tried enabling the log in IIS through the my.ini
but that didn't log anything as far as I could see.
THIS IS NOT A DUPLICATE!!!!
$stmt->execute()
returns TRUE because I am redirected to staff.php?updated
!
I also have $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
set so what you have linked does not help. Read the question before acting in haste.