我有以下代码来处理我的PHP表单:
$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>";
以下代码为自己的表单。
$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;
}
然而,尽管调试代码并调查它并不起作用。 这是代码的第一部分,它不起作用(表单提交)。 我尝试在update语句中使用debug params,但这就是我得到的:
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
为什么这段代码不起作用? staff_fields包含字段以及表单中使用的字段,因此我不知道为什么这不起作用。
我尝试通过my.ini
启用IIS中的日志,但是我没有记录任何内容。
这不是重复!!!!
$stmt->execute()
返回TRUE,因为我被重定向到staff.php?updated
!
我也设置了$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
,因此您所关联的内容无效。在急速行动之前阅读问题。
答案 0 :(得分:-1)
你可以替换:
foreach($result1 as $q){
$query .= $q['name']."=:".$q['name'].", ";
}
使用
foreach($result1 as $q){
$query .= $q['name']."='".$_POST[$q['name']]."', ";
}
您使用表单提交值但未捕获表单中发布的值。
希望这有效。
干杯