Blogger Information
Blog 29
fans 0
comment 0
visits 29219
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO预处理更新操作
咸鱼梦
Original
1261 people have browsed it
<?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();
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post