doushen2154 2014-11-12 08:40
浏览 81
已采纳

错误:使用PDO的未定义变量

I've just strated using PDO & so far I like it but I can not seem to solve this problem. I keep getting notice that error variable is not defined. I'm sure there's a stupid mistake somewhere in the code or maybe I am doing it wrong altogether.

<?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>&nbsp;</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
    }
?>
  • 写回答

1条回答 默认 最新

  • doujiejujixi27244 2014-11-12 08:43
    关注

    try changing if(!$error) to if(!isset($error))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名