一:新建文本pdo_insert.php代码如下:主要是一个表单,用户可以通过填写用户名,邮箱,密码等项目来向数据库添加数据;数据的验证是在pdo_reqinster.php文档中;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <?php
$nameTitle = 'PDO表单验证登录' ;
require 'header.php' ;
?>
<div class = "container" >
<div class = "row" >
<div class = "col-md-12" >
<h3 class = "text-center" >用户注册</h3>
<form action= "pdo_reginster.php" method= "post" class = "form-horizontal" >
<div class = "form-group" >
<label for = "name" class = "col-sm-2 control-label" >用户名:</label>
<div class = "col-sm-10" >
<input type= "text" class = "form-control" id= "name" placeholder= "userName" name= "name" value= "<?php echo (isset($_POST['name']))?$_POST['name']:''?>" >
</div>
</div>
<div class = "form-group" >
<label for = "email" class = "col-sm-2 control-label" >邮箱:</label>
<div class = "col-sm-10" >
<input type= "email" class = "form-control" id= "email" placeholder= "Email" name= "email" value= "<?php echo (isset($_POST['email']))?$_POST['email']:''?>" >
</div>
</div>
<div class = "form-group" >
<label for = "password1" class = "col-sm-2 control-label" >密码:</label>
<div class = "col-sm-10" >
<input type= "password" class = "form-control" id= "password1" placeholder= "Password" name= "password1" value= "<?php echo (isset($_POST['password1']))?$_POST['password1']:''?>" >
</div>
</div>
<div class = "form-group" >
<label for = "password2" class = "col-sm-2 control-label" >密码确认:</label>
<div class = "col-sm-10" >
<input type= "password" class = "form-control" id= "password2" name= "password2" placeholder= "Confirm Password" value= "<?echo (isset($_POST['password2']))?$_POST['password2']:''?>" >
</div>
</div>
<div class = "form-group" >
<div class = "col-sm-offset-2 col-sm-10 text-center" >
<button type= "submit" class = "btn btn-primary btn-block" >注册</button>
</div>
</div>
</form>
</div>
</div>
</div>
|
二:PDO 的新增操作即写操作涉及方法 exec();以下是pdo_reginster.php文档中的代码:主要是对pdo_insert.php表单提交过来的数据进行验证;验证通过之后再将数据写入数据库的操作:主要考察的就是Pdo的写操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php
require 'header.php' ;
if ( $_SERVER [ 'REQUEST_METHOD' ]== 'POST' ){
echo <<< 'IN' <script> if (confirm( "确认提交吗" )){
} else {
location.href= "pdo_insert.php" ;
}</script>
IN;
if ( empty ( $_POST [ 'name' ])){
echo '<script>alert("没有输入用户名");location.href="pdo_insert.php"</script>' ;
exit ();
} else { $name = $_POST [ 'name' ];
}
if ( empty ( $_POST [ 'email' ])){
echo '<script>alert("没有输入邮箱");location.href="pdo_insert.php"</script>' ;
exit ();
} else {
$email = $_POST [ 'email' ];
}
if ( empty ( $_POST [ 'password1' ])){
echo '<script>alert("没有输入密码");location.href="pdo_insert.php"</script>' ;
exit (); } elseif ( empty ( $_POST [ 'password2' ])){
echo '<script>alert("没有输入确认密码");location.href="pdo_insert.php"</script>' ;
exit ();
} elseif ( $_POST [ 'password1' ]!== $_POST [ 'password2' ]){
echo '<script>alert("两次输入密码不相等");location.href="pdo_insert.php"</script>' ; exit ();
} else {
$password = $_POST [ 'password1' ];
}
require 'pdo_connect.php' ;
$sql = "INSERT `user1` SET `name`='{$name}',`email`='{$email}',`password`=sha1('{$password}')" ;
$num = $pdo -> exec ( $sql );
if ( $num ==1){
echo '成功新增了' . $num . '条数据,新增主键id 是' . $pdo ->lastInsertId();
} else {
print_r( $pdo_errorInfo ()); }}
echo <<< 'INPUT'
<div class = "container" >
<div class = "row" >
<div class = "col-md-12" >
<h3 class = "text-center" >用户登录</h3>
<form class = "form-horizontal" action= "pdo_view.php" method= "post" > <div class = "form-group" >
<label for = "name" class = "col-sm-2 control-label" >用户名</label> <div class = "col-sm-10" >
<input type= "text" class = "form-control" id= "name" placeholder= "请输入用户名" name= "name" >
</div>
</div>
<div class = "form-group" >
<label for = "password1" class = "col-sm-2 control-label" >密码</label> <div class = "col-sm-10" >
<input type= "password" class = "form-control" id= "password1" placeholder= "请输入密码" name= "password1" >
</div>
</div>
<div class = "form-group" >
<div class = "col-sm-offset-2 col-sm-10 text-center" >
<button type= "submit" class = "btn btn-primary btn-block" >登录</button> </div>
</div>
</div>
</div>
</div>
INPUT;
?>
|
三:以下是pdo_view.php;主要考察PDO的查询操作,其中查询操作我使用的是PDO的预处理对象来做的;将查询的数据表中的信息遍历在表格中:在表格中我分别添加了'修改'和'删除'的超链接;以用户id 的方式来修改或者删除指定用户信息;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php
require 'header.php' ;
require 'pdo_connect.php' ;
try {
$sql = "SELECT * FROM `user1` WHERE `id`> :id" ;
$pdo_stmt = $pdo ->prepare( $sql ); if (true== $pdo_stmt ){
$res = $pdo_stmt ->execute([ 'id' =>2]);
if (true== $res ){
if ( $pdo_stmt ->rowCount()>0){
echo '<h2 align="center">用户信息列表</h2>' ;
echo '<table border="1" cellpading="0" cellspacing="0" style="width:800px;text-align:center;margin:0 auto;">' ;
echo '<tr bgcolor="antiquewhite" style="line-height:45px;"><td>Id</td><td>Name</td><td>Email</td><td>操作</td></tr>' ;
while ( $rows = $pdo_stmt ->fetch(PDO::FETCH_ASSOC)){
echo '<tr>' ;
echo '<td>' . $rows [ 'id' ]. '</td>' ;
echo '<td>' . $rows [ 'name' ]. '</td>' ;
echo '<td>' . $rows [ 'email' ]. '</td>' ;
echo '<td><a class="btn btn-primary" href="pdo_update.php?id=' . $rows [ 'id' ]. '" role="button">修改</a><a class="btn btn-danger" href="pdo_delete.php?id=' . $rows [ 'id' ]. '">删除</a></td>' ;
echo '</tr>' ;
} echo '</table>' ;
} else {
echo '<h2 style="color:red">数据表中没有数据</h2>' ;
}
}
} else { print_r( $pdo ->errorInfo());
}
} catch (PDOException $e ){
echo $e ->getMessage();
}
?>
|
三:以下是pdo_update.php文档中的代码:在pdo_view.php页面当点击修改按钮是跳转的就是这个页面:这个页面可以对用户的用户名,邮箱进行修改,然后提交验证是在本页面操作的;代码如下:
更新操作也是基于查询的主要有以下步骤:
1,填写要修改的用户名,邮箱点击提交按钮;
2,本页面通过POST方式获取提交的用户名,邮箱的值;通过get方式获取到用户的id;
3,进行数据验证;验证通过的话;
4,验证合格的话则开始进行用户信息更新工作;
更新的步骤如下:
1,生成一个预处理查询sql;
2,生成预处理查询对象;prepare();
3,执行查询操作: execute();以关联数组方式给SQL语句中的命名占位符绑定数据
4,获取到查询的数据解析到一维数组中;
5,生成更新语句注意更新条件的id与查询数据的id相等;
6,执行更新操作:exec();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <?php
$nameTitle = '用户信息修改' ;
require 'header.php' ;
if ( $_SERVER [ 'REQUEST_METHOD' ]== 'POST' ){
require 'pdo_connect.php' ;
$id =isset( $_GET [ 'id' ])? $_GET [ 'id' ]:null;
$email =isset( $_POST [ 'email' ])? $_POST [ 'email' ]:null;
$name =isset( $_POST [ 'name' ])? $_POST [ 'name' ]:null;
echo $name ; echo $email ;
if ( empty ( $name )){
echo '用户名为空' ;
exit ;
} else {
if ( empty ( $email )){
echo '邮箱名为空' ;
exit ;
}
}
$sql = "SELECT * FROM `user1` WHERE `id`=:id" ;
$pdo_stmt = $pdo ->prepare( $sql );
if (true== $pdo_stmt ){
$res = $pdo_stmt ->execute([ 'id' => $id ]);
if (true== $res ){
if ( $pdo_stmt ->rowCount()>0){
$row = $pdo_stmt ->fetch(PDO::FETCH_ASSOC);
$sql2 = "UPDATE `user1` SET `name`='{$name}',`email`='{$email}' WHERE `id`={$row['id']}" ;
try {
$num = $pdo -> exec ( $sql2 );
if ( $num ==1){
echo '<h2 style="color:green">成功更新了' . $num . '条数据</h2><script>location.href="pdo_view.php"</script>' ;
} else {
echo '<h2 style="color:red">数据已经更新<script>location.href="pdo_view.php"</script></h2>' ;
}
} catch (PDOException $e ){
echo $e ->getMessage();
}
} else {
echo '<h2>已经被更新</h2>' ;
}
}
} else {
echo '<h2 style="color:red">数据表中没该条有数据</h2>' ;
}
}
?>
<div class = "container" >
<div class = "row" >
<div class = "col-md-12" >
<h3 class = "text-center" >用户信息修改</h3>
<form class = "form-horizontal" action= "" method= "post" >
<div class = "form-group" >
<label for = "name" class = "col-sm-2 control-label" >用户名</label>
<div class = "col-sm-10" >
<input type= "text" class = "form-control" id= "name" placeholder= "请输入用户名" name= "name" >
</div>
</div>
<div class = "form-group" >
<label for = "email" class = "col-sm-2 control-label" >邮箱</label>
<div class = "col-sm-10" >
<input type= "text" class = "form-control" id= "email" placeholder= "请输入用户名" name= "email" >
</div>
</div>
<div class = "form-group" >
<div class = "col-sm-offset-2 col-sm-10 text-center" >
<button type= "submit" class = "btn btn-primary btn-block" >提交</button>
</div>
</div>
</div>
</div>
</div>
|
四:以下是在pdo_view.php页面点击删除按钮是页面跳转的地址;pdo_delete.php的文档;
主要考察的是PDO对象的删除操作;删除操作主要有以下步骤;*(在这个文档中我的查询操作使用的是
PDO预处理操作);
1,get方式获取指定的用户id;
2,连接数据库:pdo_connect.php是已经封装好的pdo数据库连接操作;
3,生成查询语句(删除是基于查询的);
4,执行查询功能:查询成功才能继续删除工作否则用该提示用户没有改条信息
5,将查询到的数据赋值到一维数组中;
6,生成删除语句;
7,执行删除功能:成功则提示成功exec()返回值为1 时,否则该语句已经被删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php
$id =isset( $_GET [ 'id' ])? $_GET [ 'id' ]:null;
echo <<< 'IN'
<script>
if (confirm( "确认删除吗" )){
} else {
location.href= "viewi.php" ;
}</script>IN;
require 'pdo_connect.php' ;
try {
$sql = "SELECT `id`,`name`,`password`,`email` FROM `user1` WHERE `id`=:id" ;
if (true== $PDOstament ){
$res = $PDOstament ->execute([ 'id' => $id ]);
if (true== $res ){
if ( $PDOstament ->rowCount()==1){
$row = $PDOstament ->fetch(PDO::FETCH_ASSOC);
$num = $pdo -> exec ( "DELETE from user1 WHERE id=" . $row [ 'id' ]); i
f( $num ==1){ echo '<h2 style="color:green">删除成功</h2>' ;
} else {
echo '<h2 style="color:red">删除失败</h2>' ;
}
} else {
echo '没有该条数据或者已经被删除' ;
}
} else {
echo '没有该条数据或者已经被删除' ;
}
} else {
print_r( $pdo ->errorInfo());
}
} catch (PDOException $e ){
$e ->getMessage();
}
?>
|