我刚刚使用PDO& amp;到目前为止,我喜欢它,但我似乎无法解决这个问题。我一直注意到没有定义错误变量。我确定代码中某处存在一个愚蠢的错误,或者说我完全错了。
<?php require_once 'includes/layouts/header.php'; ?>
<?php require_once 'includes/functions.php'; ?>
<?php
try {
require_once 'Includes/pdo_connect.php';
$stmt=$db->prepare('SELECT * FROM employees WHERE `Emp_id` = :edit_id');
$stmt -> bindParam(':edit_id', $_GET['id'], PDO::PARAM_INT);
$stmt -> execute();
$result = $stmt-> fetchAll(PDO::FETCH_ASSOC);
$errorInfo = $db->errorInfo();
if (isset($errorInfo[2])) {
$error = $errorInfo[2];
}
} catch (Exception $e) {
$error = $e->getMessage();
}
?>
<?php
if(isset($_POST['submit'])) {
if(!empty($_POST['department']) && !empty($_POST['email']) && !empty($_POST['name'])) {
try {
require_once 'Includes/pdo_connect.php';
$stmt = $db->prepare('UPDATE `employees` SET Department= :Department, Email= :Email, Name = :Name
WHERE Emp_id= :id LIMIT 1');
$stmt->bindParam(':Department', $_POST['department'], PDO::PARAM_STR);
$stmt->bindParam(':Email', $_POST['email'], PDO::PARAM_STR);
$stmt->bindParam(':Name', $_POST['name'], PDO::PARAM_STR);
$stmt->bindParam(':id', $row['id'], PDO::PARAM_INT);
$stmt->execute();
$errorInfo = $stmt->errorInfo();
if(isset($errorInfo[2])) {
$error= $errorInfo[2];
}
if(!$error){
redirect_to('manage_employees.php');
}
} catch (Exception $e) {
$error = $e->getMessage();
}
} else {
echo "Department, Email and Name can not be blank, please try again!";
}
}
if(isset($error)) {
echo $error;
}
?>
<?php
foreach($result as $row) {
?>
<form action="edit_employee.php?id=<?php echo urlencode($row["Emp_id"]); ?>" method="post" enctype="multipart/form-data" name="form1" id="form1" autocomplete="off">
<p> </p>
<table width="30%" border="1" cellspacing="0" cellpadding="5">
<tr>
<th colspan="2">Edit employee:</th>
</tr>
<tr>
<td width="36%"><strong>Department</strong></td>
<td width="64%"><select name="department" id="department" >
<?php
$dept_list = array(
'Administration' => 'Administration',
'Billing and IT' => 'Billing and IT',
'Finance' => 'Finance',
'Human Resources' => 'Human Resources',
'Marketing' => 'Marketing',
'Planning' => 'Planning',
'Technical' => 'Technical',
);
foreach($dept_list as $key => $value) {
$selected = "";
if(isset($row)) {
if($row['Department'] == $key ) {
$selected = 'selected="selected"';
}
}
?>
<option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $value; ?></option>
<?php
}
?>
</select></label></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td><input size="30" type="email" name="email" id="email" value="<?php echo $row['Email']; ?>"/></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td><input size="30" type="text" name="name" id="name" value="<?php echo $row['Name']; ?>"/></td>
</tr>
<tr>
<th colspan="2"><label>
<input type="submit" name="submit" id="submit" value="Save"/>
</label></th>
</tr>
</table>
</form>
<?php
}
?>
答案 0 :(得分:1)
尝试将if(!$error)
更改为if(!isset($error))