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 智能404跳转代码,适合换域名没改变目录的网站
Jun 04 PHP
一个基于PDO的数据库操作类
Mar 24 PHP
PHP设计模式之责任链模式的深入解析
Jun 13 PHP
解析PHP生成静态html文件的三种方法
Jun 18 PHP
header导出Excel应用示例
Jan 24 PHP
php+jQuery+Ajax简单实现页面异步刷新
Aug 08 PHP
Thinkphp5.0自动生成模块及目录的方法详解
Apr 17 PHP
thinkphp下MySQL数据库读写分离代码剖析
Apr 18 PHP
在Yii2特定页面如何禁用调试工具栏Debug Toolbar详解
Aug 07 PHP
laravel 实现设置时区的简单方法
Oct 10 PHP
Laravel中如何轻松容易的输出完整的SQL语句
Jul 26 PHP
PHP之header函数详解
Mar 02 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学习笔记 数组的常用函数
2011/06/13 PHP
php中如何同时使用session和cookie来保存用户登录信息
2013/07/05 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
2013/08/07 PHP
ThinkPHP模板判断输出Present标签用法详解
2014/06/30 PHP
PHP程序员常见的40个陋习,你中了几个?
2014/11/20 PHP
php的4种常见运行方式
2015/03/20 PHP
php实现通过soap调用.Net的WebService asmx文件
2017/02/27 PHP
基于jQuery的表格操作插件
2010/04/22 Javascript
原生js做的手风琴效果的导航菜单
2013/11/08 Javascript
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
jQuery判断checkbox是否选中的小例子
2013/12/02 Javascript
同域jQuery(跨)iframe操作DOM(示例代码)
2013/12/13 Javascript
在HTML代码中使用JavaScript代码的例子
2014/10/16 Javascript
JS实现转动随机数抽奖特效代码
2020/04/16 Javascript
javascript实现鼠标放上后下边对应内容变换的效果
2015/08/06 Javascript
AngularJS的ng-repeat指令与scope继承关系实例详解
2017/01/21 Javascript
vue基于Vue2.0和高德地图的地图组件实例
2017/04/28 Javascript
JS实现的加减乘除四则运算计算器示例
2017/08/09 Javascript
使用layui的router来进行传参的实现方法
2019/09/06 Javascript
[43:41]VP vs RNG 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.21.mp4
2020/07/19 DOTA
python解析模块(ConfigParser)使用方法
2013/12/10 Python
使用Python的Flask框架实现视频的流媒体传输
2015/03/31 Python
Python构建网页爬虫原理分析
2017/12/19 Python
人脸识别经典算法一 特征脸方法(Eigenface)
2018/03/13 Python
解决pycharm启动后总是不停的updating indices...indexing的问题
2019/11/27 Python
Python request操作步骤及代码实例
2020/04/13 Python
Django 5种类型Session使用方法解析
2020/04/29 Python
聊聊python中的循环遍历
2020/09/07 Python
CSS3实现文字波浪线效果示例代码
2016/11/20 HTML / CSS
Tory Burch美国官方网站:美国时尚生活品牌
2016/08/01 全球购物
彪马法国官网:PUMA法国
2019/12/15 全球购物
中学教师培训制度
2014/01/31 职场文书
小学生母亲节演讲稿
2014/05/07 职场文书
产品委托授权书范本
2014/09/16 职场文书
导游词之任弼时故居
2020/01/07 职场文书
2022漫威和DC电影上映作品
2022/04/05 欧美动漫