当前位置: 首页 > 编程笔记 >

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

雍飞雨
2023-03-14
本文向大家介绍PHP连接数据库实现注册页面的增删改查操作,包括了PHP连接数据库实现注册页面的增删改查操作的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了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>

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");
?>

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>&nbsp;&nbsp;&nbsp;&nbsp;<a href='delete.php?id=$value[id]'>删除</a></td>";
 echo "<tr/>";
 echo "</table>";
}
?>

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
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('修改失败')";
}
?>

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('删除失败')";
}
?>
//若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除

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

 类似资料:
  • 本文向大家介绍java jdbc连接mysql数据库实现增删改查操作,包括了java jdbc连接mysql数据库实现增删改查操作的使用技巧和注意事项,需要的朋友参考一下 jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打交道非常基础的一个知识,也是比较接近底层的,在实际的工作中大家用得更多的其实还是比

  • 本文向大家介绍php实现数据库的增删改查,包括了php实现数据库的增删改查的使用技巧和注意事项,需要的朋友参考一下 1.查询: 数据的显示,这里就可以嵌入php来进行数据的输出 2.删除的处理页面 删除时是链接到删除处理页面的,所以还要写一个删除处理页面: 来张效果图: 3.添加数据: 点击即可进入添加页面 添加页面: 添加也需要一个处理页面来判断添加: 效果图: 4.修改数据:主键不可修改!!

  • 本文向大家介绍Python实现连接MySql数据库及增删改查操作详解,包括了Python实现连接MySql数据库及增删改查操作详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Python实现连接MySql数据库及增删改查操作。分享给大家供大家参考,具体如下: 在本文中介绍 Python3 使用PyMySQL连接数据库,并实现简单的增删改查。(注意是python3) 1、安装PyMySQ

  • 本文向大家介绍PHP连接MySQL进行增、删、改、查操作,包括了PHP连接MySQL进行增、删、改、查操作的使用技巧和注意事项,需要的朋友参考一下 话不多说,请看代码: fetch_all()              返回全部数组 fetch_row()            返回索引数组 fetch_assoc()         返回关联数组 fetch_object()        返回

  • 本文向大家介绍php中PDO方式实现数据库的增删改查,包括了php中PDO方式实现数据库的增删改查的使用技巧和注意事项,需要的朋友参考一下 需要开启php的pdo支持,php5.1以上版本支持 实现数据库连接单例化,有三要素 静态变量、静态实例化方法、私有构造函数 DPDO.php 用于处理字段映射,使用pdo的字段映射,可以有效避免sql注入 增删改查的具体实现DB.php 测试文件test.p

  • 一、前言 在第二章《Memcached源码分析 - Memcached源码分析之命令解析(2)》 和第三章《Memcached源码分析 - Memcached源码分析之消息回应(3)》 中我们主要通过Memcached的get命令,分析了Memcached的命令解析和消息回应的模块功能。这一章,我们主要来详细看一下Memcached常用的增删改查操作。 在看Memcached的增删改查操作前,我们