首页 > 开发 > PHP > 正文

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

2024-05-04 22:01:40
字体:
来源:转载
供稿:网友

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

1.连接数据库

  1. <?php 
  2.  
  3.  //本地测试 
  4.  
  5.  $host = '127.0.0.1'
  6.  
  7.  $port = 3306; 
  8.  
  9.  $user = "root"
  10.  
  11.  $pwd = ""
  12.  
  13.  $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true); 
  14.  
  15.  if(!$link) { 
  16.  
  17.   die("Connect Server Failed: " . mysql_error()); 
  18.  
  19.  } 
  20.  
  21.  //选择连接的数据库库名 
  22.  
  23.  mysql_select_db("my"); 
  24.  
  25.  //设置字符编码utf8 
  26.  
  27.  mysql_set_charset('utf8'); 
  28.  
  29. ?> 

2.注册页面(html页面) 

  1. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
  2.  
  3.  <title>Document</title> 
  4.  
  5. <h3>注册页面</h3> 
  6.  
  7.  <form action="add.php" method="post"> 
  8.  
  9.   <table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF"> 
  10.  
  11.    <tbody><tr> 
  12.  
  13.     <td align="right">用户名</td> 
  14.  
  15.     <td><input type="text" name="username" id="">以小写字母开始,长度要求5~10</td> 
  16.  
  17.    </tr> 
  18.  
  19.    <tr> 
  20.  
  21.     <td align="right">密码</td> 
  22.  
  23.     <td><input type="password" name="password" id="">密码不能为空</td> 
  24.  
  25.    </tr> 
  26.  
  27.    <tr> 
  28.  
  29.     <td align="right">邮箱</td> 
  30.  
  31.     <td><input type="text" name="email" id=""></td> 
  32.  
  33.    </tr> 
  34.  
  35.    <tr> 
  36.  
  37.     <td align="right">性别</td> 
  38.  
  39.     <td> 
  40.  
  41.      <input type="radio" name="sex" id="" value="1">男 
  42.  
  43.      <input type="radio" name="sex" id="" value="2">女 
  44.  
  45.      <input type="radio" name="sex" id="" value="3">保密 
  46.  
  47.     </td> 
  48.  
  49.    </tr> 
  50.  
  51.    <tr> 
  52.  
  53.     <td align="right">个人简介</td> 
  54.  
  55.     <td> 
  56.  
  57.      <textarea name="txt" id="" cols="50" rows="10"></textarea> 
  58.  
  59.     </td> 
  60.  
  61.    </tr> 
  62.  
  63.    <tr> 
  64.  
  65.     <td colspan="2"><input type="submit" name="act" value="注册"></td> 
  66.  
  67.    </tr> 
  68.  
  69.   </tbody></table> 
  70.  
  71.  </form> 

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

  1. //往数据库中添加数据 
  2.  
  3. <?php 
  4.  
  5. header("Content-type:text/html; charset=utf-8"); 
  6.  
  7. //-----------------------连接数据库--------------------------- 
  8.  
  9. include_once "connect.php"
  10.  
  11. //-------------------------将数据连接到数据库------------------ 
  12.  
  13. $time=time(); 
  14.  
  15. $sql="insert into user (username,password,email,sex,txt,`time`) value('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}','{$_POST['sex']}','{$_POST['txt']}','{$time}')"
  16.  
  17. $res=mysql_query($sql); 
  18.  
  19. header("location:hello.php"); 
  20.  
  21. ?> 

4.返回后台界面

  1. <?php 
  2.  
  3. header("Content-type:text/html; charset=utf-8"); 
  4.  
  5. //-----------------------连接数据库------------------------------ 
  6.  
  7. include_once "connect.php"
  8.  
  9. //--------------------查询数据库-------------------------------- 
  10.  
  11. $query="select * from user"
  12.  
  13. $result=mysql_query($query); 
  14.  
  15. if(!$result
  16.  
  17.  
  18.  die("could not to the database<br>".mysql_error()); 
  19.  
  20.  
  21. //-------------------封装函数----------------------------- 
  22.  
  23. //该函数将数据库的数据写成数组形式 
  24.  
  25. function result2Arr($result){ 
  26.  
  27.  while($result_row=mysql_fetch_assoc($result)){ 
  28.  
  29.   $arr[] = $result_row
  30.  
  31.  } 
  32.  
  33.  return $arr
  34.  
  35.  
  36. $arr = result2Arr($result); 
  37.  
  38. foreach($arr as $key=>$value){ 
  39.  
  40.  echo ""
  41.  
  42.  echo "<table border="1px"></table>"
  43.  
  44.  echo " "
  45.  
  46.  echo ""
  47.  
  48.  echo ""
  49.  
  50.  echo ""
  51.  
  52.  echo ""
  53.  
  54.  echo ""
  55.  
  56.  echo ""
  57.  
  58.  echo ""
  59.  
  60.  echo ""
  61. //Vevb.com 
  62.  echo ""
  63.  
  64.  echo "<table border="1px"><tbody><tr><td width="100px">".$value['id']."</td><td width="100px">".$value['username']."</td><td width="100px">".$value['password']."</td><td width="200px">".$value['email']."</td><td width="100px">".$value['sex']."</td><td width="100px">".$value['txt']."</td><td width="100px">".date('Y-m-d H:i:s',$value['time'])."</td><td width="100px"><a href="update1.php?id=$value[id]">修改</a>    <a href="delete.php?id=$value[id]">删除</a></td></tr><tr></tr></tbody></table>"
  65.  
  66.  
  67. ?> 

5.修改数据

  1. //当用户要修改信息时,返回页面,页面中包含之前填写的信息 
  2.  
  3.  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
  4.  
  5.  <title>Document</title> 
  6.  
  7. <div> 
  8.  
  9. <?php 
  10.  
  11.  include_once "connect.php"
  12.  
  13.  $sql="select * from user where id='".$_GET['id']."'"; 
  14.  
  15.  //echo "sql:".$sql;(显示出修改哪一行) 
  16.  
  17.  $result=mysql_query($sql,$link); 
  18.  
  19.  $arr = result2Arr($result); 
  20.  
  21.  //print_r($arr); 
  22.  
  23.  $row = $arr[0]; 
  24.  
  25. function result2Arr($result){ 
  26.  
  27.  while($result_row=mysql_fetch_assoc($result)){ 
  28.  
  29.   $arr[] = $result_row
  30.  
  31.  } 
  32.  
  33.  return $arr
  34.  
  35.  
  36. ?> 
  37.  
  38.   <h3>注册页面</h3> 
  39.  
  40.   <form action="update.php" method="post"
  41.  
  42.    <input type="hidden" name="id" id="" value="<?php echo $row['id']?>"
  43.  
  44.    <table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF"
  45.  
  46.     <tbody><tr> 
  47.  
  48.      <td align="right">用户名</td> 
  49.  
  50.      <td><input type="text" name="username" id="" value="<?php echo $row['username']?>">以小写字母开始,长度要求5~10</td> 
  51.  
  52.     </tr> 
  53.  
  54.     <tr> 
  55.  
  56.      <td align="right">密码</td> 
  57.  
  58.      <td><input type="password" name="password" id="" value="<?php echo $row['password']?>">密码不能为空</td> 
  59.  
  60.     </tr> 
  61.  
  62.     <tr> 
  63.  
  64.      <td align="right">邮箱</td> 
  65.  
  66.      <td><input type="text" name="email" id="" value="<?php echo $row['email']?>"></td> 
  67.  
  68.     </tr> 
  69.  
  70.     <tr> 
  71.  
  72.      <td align="right">性别</td> 
  73.  
  74.      <td> 
  75.  
  76.       <input type="radio" name="sex" id="" value="1" <?php="" if($row['sex']="='1'){" echo="" 'checked';}?="">/>男 
  77.  
  78.       <input type="radio" name="sex" id="" value="2" <?php="" if($row['sex']="='2'){" echo="" 'checked';}?="">/>女 
  79.  
  80.       <input type="radio" name="sex" id="" value="3" <?php="" if($row['sex']="='3'){" echo="" 'checked';}?="">/>保密 
  81.  
  82.      </td> 
  83.  
  84.     </tr> 
  85.  
  86.     <tr> 
  87.  
  88.      <td align="right">个人简介</td> 
  89.  
  90.      <td> 
  91.  
  92.       <textarea name="txt" id="" cols="50" rows="10"><?php echo $row['txt']?></textarea> 
  93.  
  94.      </td> 
  95.  
  96.     </tr> 
  97.  
  98.     <tr> 
  99.  
  100.      <td colspan="2"><input type="submit" name="act" value="修改"></td> 
  101.  
  102.     </tr> 
  103.  
  104.    </tbody></table> 
  105.  
  106.   </form> 
  107.  
  108. </div> 
  109.  
  110. //将修改的信息存入数据库 
  111.  
  112. <?php 
  113.  
  114. header("Content-type:text/html; charset=utf-8"); 
  115.  
  116. //通过post获取页面提交数据信息 
  117.  
  118. $data = $_POST
  119.  
  120. //print_r($data); 
  121.  
  122. include_once "connect.php"
  123.  
  124. $sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'"
  125.  
  126. echo $sql
  127.  
  128. $res = mysql_query($sql,$link); 
  129.  
  130. if($res){ 
  131.  
  132.  header("Location:hello.php"); 
  133.  
  134.  //echo "alert('修改成功')"; 
  135.  
  136. }else
  137.  
  138.  header("Location:update1.php?id=".$data['id']); 
  139.  
  140.  //echo "alert('修改失败')"; 
  141.  
  142.  
  143. ?> 

6.删除数据 

  1. //删除数据库里的数据 
  2.  
  3. <?php 
  4.  
  5. header("Content-type:text/html; charset=utf-8"); 
  6.  
  7. include_once 'connect.php'
  8.  
  9. $sql = "delete from user where id='".$_GET['id']."'"; 
  10.  
  11. $sus=mysql_query($sql,$link); 
  12.  
  13. if($sus){ 
  14.  
  15.  header("location:hello.php"); 
  16. //Vevb.com 
  17. }else
  18.  
  19.  echo "alert('删除失败')"
  20.  
  21.  
  22. ?> 

若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表