<?php try { $dsn = 'mysql:dbname=demo'; $userName = 'root'; $password = 'root'; $pdo = new PDO($dsn,$userName,$password); //创建sql预处理语句 $sql = "UPDATE `user` SET `name`=:name,`email`=:email,`password`=sha1(:password) WHERE `id`=:id"; //执行sql预处理语句,创建一个PDOStatement $pdoStmt = $pdo->prepare($sql); if ($pdoStmt == true) { //新建更新数据 $data = ['name'=>'更新','email'=>'gx@qq.com','password'=>'546','id'=>6]; //bindParam()第一个参数必须是命名占位符,不能是字面量 $pdoStmt->bindParam(':name',$data['name']); $pdoStmt->bindParam(':email',$data['email']); $pdoStmt->bindParam(':password',$data['password']); $pdoStmt->bindParam(':id',$data['id']); //执行预处理语句 $res = $pdoStmt->execute($data); if ($res == true) { echo '更新成功!'; } else { echo '更新失败。'; } } else { print_r($pdo->errorInfo()); } } catch(PDOException $e) { print 'Connect ERROR'.$e->getMessage(); dre(); }