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 相关文章推荐
PHP之变量、常量学习笔记
Mar 27 PHP
PHP变量内存分配问题记录整理
Nov 27 PHP
CodeIgniter实现更改view文件夹路径的方法
Jul 04 PHP
推荐25款php中非常有用的类库
Sep 29 PHP
php生成百度sitemap站点地图类函数实例
Oct 17 PHP
Yii核心组件AssetManager原理分析
Dec 02 PHP
PHP中利用sleep函数实现定时执行功能实现代码
Aug 25 PHP
PHP实现原生态图片上传封装类方法
Nov 08 PHP
PHP清除缓存的几种方法总结
Sep 12 PHP
PHP获取链表中倒数第K个节点的方法
Jan 18 PHP
PHP实现微信红包金额拆分试玩的算法示例
Apr 07 PHP
PHP单元测试配置与使用方法详解
Dec 27 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
利用文件属性结合Session实现在线人数统计
2006/10/09 PHP
mysql时区问题
2008/03/26 PHP
PHP版国家代码、缩写查询函数代码
2011/08/14 PHP
yii2.0框架多模型操作示例【添加/修改/删除】
2020/04/13 PHP
关于javascript中的parseInt使用技巧
2009/09/03 Javascript
js设置控件的隐藏与显示的两种方法
2014/08/21 Javascript
Javascript中的数据类型之旅
2015/10/18 Javascript
JavaScript的Backbone.js框架入门学习指引
2016/05/07 Javascript
JS基于HTML5的canvas标签实现炫目的色相球动画效果实例
2016/08/24 Javascript
Move.js入门
2017/02/08 Javascript
详解JavaScript中js对象与JSON格式字符串的相互转换
2017/02/14 Javascript
jQuery实现ajax的嵌套请求案例分析
2019/02/16 jQuery
解决vue 单文件组件中样式加载问题
2019/04/24 Javascript
JS使用正则表达式判断输入框失去焦点事件
2019/10/16 Javascript
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
Python写的一个简单监控系统
2015/06/19 Python
Python基于sklearn库的分类算法简单应用示例
2018/07/09 Python
详解python while 函数及while和for的区别
2018/09/07 Python
PyQt5 多窗口连接实例
2019/06/19 Python
对python中基于tcp协议的通信(数据传输)实例讲解
2019/07/22 Python
Python unittest工作原理和使用过程解析
2020/02/24 Python
CSS3实现鼠标悬停显示扩展内容
2016/08/24 HTML / CSS
ALDO加拿大官网:加拿大女鞋品牌
2018/12/22 全球购物
Myprotein法国官网:欧洲第一运动营养品牌
2019/03/26 全球购物
CLR与IL分别是什么含义
2016/08/23 面试题
车辆安全检查制度
2014/01/12 职场文书
运动会表扬稿大全
2014/01/16 职场文书
保护环境的建议书
2014/03/12 职场文书
太太口服液广告词
2014/03/20 职场文书
高中学生期末评语
2014/04/25 职场文书
毕业生应聘求职信
2014/07/10 职场文书
三好学生先进事迹材料
2014/08/28 职场文书
农民工预备党员思想汇报
2014/09/14 职场文书
2014年公务员个人工作总结
2014/11/22 职场文书
建筑工地资料员岗位职责
2015/04/13 职场文书
团结友爱主题班会
2015/08/13 职场文书