PHP连接数据库实现注册页面的增删改查操作


Posted in PHP onMarch 27, 2016

本文实例为大家分享了PHP连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下

1.连接数据库

<?php
 //本地测试
 $host = '127.0.0.1';
 $port = 3306;
 $user = "root";
 $pwd = "";
 $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
 if(!$link) {
  die("Connect Server Failed: " . mysql_error());
 }
 //选择连接的数据库库名
 mysql_select_db("my");
 //设置字符编码utf8
 mysql_set_charset('utf8');
?>

2.注册页面(html页面)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <title>Document</title>
</head>
<body>
<h3>注册页面</h3>
 <form action="add.php" method='post'>
  <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
   <tr>
    <td align='right'>用户名</td>
    <td><input type="text" name="username" id=""/>以小写字母开始,长度要求5~10</td>
   </tr>
   <tr>
    <td align='right'>密码</td>
    <td><input type="password" name="password" id=""/>密码不能为空</td>
   </tr>
   <tr>
    <td align='right'>邮箱</td>
    <td><input type="text" name="email" id="" /></td>
   </tr>
   <tr>
    <td align='right'>性别</td>
    <td>
     <input type="radio" name="sex" id="" value='1' />男
     <input type="radio" name="sex" id="" value='2' />女
     <input type="radio" name="sex" id="" value='3' />保密
    </td>
   </tr>
   <tr>
    <td align='right'>个人简介</td>
    <td>
     <textarea name="txt" id="" cols="50" rows="10"></textarea>
    </td>
   </tr>
   <tr>
    <td colspan='2'><input type="submit" name='act' value='注册' /></td>
   </tr>
  </table>
 </form>

</body>
</html>

PHP连接数据库实现注册页面的增删改查操作

3.将注册数据显示在数据库

//往数据库中添加数据

<?php
header("Content-type:text/html; charset=utf-8");
//-----------------------连接数据库---------------------------
include_once "connect.php";
//-------------------------将数据连接到数据库------------------
$time=time();
$sql="insert into user (username,password,email,sex,txt,`time`) value('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}','{$_POST['sex']}','{$_POST['txt']}','{$time}')";
$res=mysql_query($sql);
header("location:hello.php");
?>

PHP连接数据库实现注册页面的增删改查操作

4.返回后台界面

<?php
header("Content-type:text/html; charset=utf-8");
//-----------------------连接数据库------------------------------
include_once "connect.php";
//--------------------查询数据库--------------------------------
$query="select * from user";
$result=mysql_query($query);
if(!$result)
{
 die("could not to the database<br/>".mysql_error());
}
//-------------------封装函数-----------------------------
//该函数将数据库的数据写成数组形式
function result2Arr($result){
 while($result_row=mysql_fetch_assoc($result)){
  $arr[] = $result_row;
 }
 return $arr;
}
$arr = result2Arr($result);
foreach($arr as $key=>$value){
 echo "<table border='1px'>";
 echo "<table border='1px' >";
 echo "<tr> ";
 echo "<td width='100px'>".$value['id']."</td>";
 echo "<td width='100px'>".$value['username']."</td>";
 echo "<td width='100px'>".$value['password']."</td>";
 echo "<td width='200px'>".$value['email']."</td>";
 echo "<td width='100px'>".$value['sex']."</td>";
 echo "<td width='100px'>".$value['txt']."</td>";
 echo "<td width='100px'>".date('Y-m-d H:i:s',$value['time'])."</td>";
 echo "<td width='100px'><a href='update1.php?id=$value[id]'>修改</a>    <a href='delete.php?id=$value[id]'>删除</a></td>";
 echo "<tr/>";
 echo "</table>";
}
?>

PHP连接数据库实现注册页面的增删改查操作

5.修改数据

//当用户要修改信息时,返回页面,页面中包含之前填写的信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <title>Document</title>
</head>
<body>
<div>

<?php
 include_once "connect.php";
 $sql="select * from user where id='".$_GET['id']."'";
 //echo "sql:".$sql;(显示出修改哪一行)
 $result=mysql_query($sql,$link);
 $arr = result2Arr($result);
 //print_r($arr);
 $row = $arr[0];

function result2Arr($result){
 while($result_row=mysql_fetch_assoc($result)){
  $arr[] = $result_row;
 }
 return $arr;
}
?>
  <h3>注册页面</h3>
  <form action="update.php" method='post'>
   <input type="hidden" name="id" id="" value="<?php echo $row['id']?>"/>
   <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
    <tr>
     <td align='right'>用户名</td>
     <td><input type="text" name="username" id="" value="<?php echo $row['username']?>"/>以小写字母开始,长度要求5~10</td>
    </tr>
    <tr>
     <td align='right'>密码</td>
     <td><input type="password" name="password" id=""value="<?php echo $row['password']?>"/>密码不能为空</td>
    </tr>
    <tr>
     <td align='right'>邮箱</td>
     <td><input type="text" name="email" id="" value="<?php echo $row['email']?>"/></td>
    </tr>
    <tr>
     <td align='right'>性别</td>
     <td>
      <input type="radio" name="sex" id="" value='1' <?php if($row['sex']=='1'){ echo 'checked';}?>/>男
      <input type="radio" name="sex" id="" value='2' <?php if($row['sex']=='2'){ echo 'checked';}?>/>女
      <input type="radio" name="sex" id="" value='3' <?php if($row['sex']=='3'){ echo 'checked';}?>/>保密
     </td>
    </tr>
    <tr>
     <td align='right'>个人简介</td>
     <td>
      <textarea name="txt" id="" cols="50" rows="10"><?php echo $row['txt']?></textarea>
     </td>
    </tr>
    <tr>
     <td colspan='2'><input type="submit" name='act' value='修改' /></td>
    </tr>
   </table>
  </form>
</div>
</body>
</html>

PHP连接数据库实现注册页面的增删改查操作

//将修改的信息存入数据库

<?php
header("Content-type:text/html; charset=utf-8");
//通过post获取页面提交数据信息
$data = $_POST;
//print_r($data);
include_once "connect.php";
$sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'";
echo $sql;
$res = mysql_query($sql,$link);
if($res){
 header("Location:hello.php");
 //echo "alert('修改成功')";
}else{
 header("Location:update1.php?id=".$data['id']);
 //echo "alert('修改失败')";
}
?>

PHP连接数据库实现注册页面的增删改查操作

6.删除数据

//删除数据库里的数据

<?php
header("Content-type:text/html; charset=utf-8");
include_once 'connect.php';
$sql = "delete from user where id='".$_GET['id']."'";
$sus=mysql_query($sql,$link);
if($sus){
 header("location:hello.php");
}else{
 echo "alert('删除失败')";
}
?>
//若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除

PHP连接数据库实现注册页面的增删改查操作

以上就是本文的全部内容,希望对大家的学习有所帮助。

PHP 相关文章推荐
PHP5 安装方法
Oct 09 PHP
供参考的 php 学习提高路线分享
Oct 23 PHP
php _autoload自动加载类与机制分析
Feb 10 PHP
介绍一些PHP判断变量的函数
Apr 24 PHP
PHP笔记之:日期函数的使用介绍
Apr 24 PHP
php下拉选项的批量操作的实现代码
Oct 14 PHP
destoon整合UCenter图文教程
Jun 21 PHP
PHP中单引号与双引号的区别分析
Aug 19 PHP
写一段简单的PHP建立文件夹代码
Jan 06 PHP
PHP中文乱码解决方案
Mar 05 PHP
PHP环境搭建的详细步骤
Jun 30 PHP
PHP命名空间简单用法示例
Dec 28 PHP
php编程中echo用逗号和用点号连接的区别
Mar 26 #PHP
php ci 获取表单中多个同名input元素值的代码
Mar 25 #PHP
PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
Mar 25 #PHP
php用正则判断是否为数字的方法
Mar 25 #PHP
PHP判断FORM表单或URL参数来的数据是否为整数的方法
Mar 25 #PHP
PHP程序员的技术成长规划
Mar 25 #PHP
php如何控制用户对图片的访问 PHP禁止图片盗链
Mar 25 #PHP
You might like
php 信息采集程序代码
2009/03/17 PHP
PHP实现读取一个1G的文件大小
2013/08/24 PHP
PHP Reflection API详解
2015/05/12 PHP
PHP里的单例类写法实例
2015/06/25 PHP
详解PHP匿名函数与注意事项
2016/03/29 PHP
PHP将MySQL的查询结果转换为数组并用where拼接的示例
2016/05/13 PHP
JCalendar 日历控件 v1.0 beta[兼容IE&amp;Firefox] 有文档和例子
2007/05/30 Javascript
JavaScript实现从数组中选出和等于固定值的n个数
2014/09/03 Javascript
在Google 地图上实现做的标记相连接
2015/01/05 Javascript
jQuery包裹节点用法完整示例
2016/09/13 Javascript
livereload工具实现前端可视化开发【推荐】
2016/12/23 Javascript
JS实现数组去重复值的方法示例
2017/02/18 Javascript
jQuery自动或手动图片切换效果
2017/10/11 jQuery
vue 2.0 购物车小球抛物线的示例代码
2018/02/01 Javascript
迅速了解一下ES10中Object.fromEntries的用法使用
2019/03/05 Javascript
微信小程序选择图片控件
2021/01/19 Javascript
简单理解Python中的装饰器
2015/07/31 Python
win10系统中安装scrapy-1.1
2016/07/03 Python
Python获取CPU、内存使用率以及网络使用状态代码
2018/02/08 Python
在python中pandas的series合并方法
2018/11/12 Python
python浪漫表白源码
2019/04/05 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
使用Python求解带约束的最优化问题详解
2020/02/11 Python
python实现opencv+scoket网络实时图传
2020/03/20 Python
动态设置django的model field的默认值操作步骤
2020/03/30 Python
人事行政主管岗位职责
2013/12/22 职场文书
入党自我评价优缺点
2014/01/25 职场文书
临床护理求职信
2014/04/26 职场文书
校本教研活动总结
2014/07/01 职场文书
受伤赔偿协议书
2014/09/24 职场文书
乡镇党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
食品质检员岗位职责
2015/04/08 职场文书
财务统计员岗位职责
2015/04/14 职场文书
歌咏比赛口号大全
2015/12/25 职场文书
java代码实现空间切割
2022/01/18 Java/Android
Win10玩csgo闪退如何解决?Win10玩csgo闪退的解决方法
2022/07/23 数码科技