首页 > 开发 > 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.  
  19.  die("could not to the database<br>".mysql_error()); 
  20.  
  21. } 
  22.  
  23. //-------------------封装函数----------------------------- 
  24.  
  25. //该函数将数据库的数据写成数组形式 
  26.  
  27. function result2Arr($result){ 
  28.  
  29.  while($result_row=mysql_fetch_assoc($result)){ 
  30.  
  31.   $arr[] = $result_row; 
  32.  
  33.  } 
  34.  
  35.  return $arr; 
  36.  
  37. } 
  38.  
  39. $arr = result2Arr($result); 
  40.  
  41. foreach($arr as $key=>$value){ 
  42.  
  43.  echo ""; 
  44.  
  45.  echo "<table border="1px"></table>"; 
  46.  
  47.  echo " "; 
  48.  
  49.  echo ""; 
  50.  
  51.  echo ""; 
  52.  
  53.  echo ""; 
  54.  
  55.  echo ""; 
  56.  
  57.  echo ""; 
  58.  
  59.  echo ""; 
  60.  
  61.  echo ""; 
  62.  
  63.  echo ""; 
  64. //Vevb.com 
  65.  echo ""; 
  66.  
  67.  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>"; 
  68.  
  69. } 
  70.  
  71. ?> 

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.  
  39.   <h3>注册页面</h3> 
  40.  
  41.   <form action="update.php" method="post"> 
  42.  
  43.    <input type="hidden" name="id" id="" value="<?php echo $row['id']?>"> 
  44.  
  45.    <table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF"> 
  46.  
  47.     <tbody><tr> 
  48.  
  49.      <td align="right">用户名</td> 
  50.  
  51.      <td><input type="text" name="username" id="" value="<?php echo $row['username']?>">以小写字母开始,长度要求5~10</td> 
  52.  
  53.     </tr> 
  54.  
  55.     <tr> 
  56.  
  57.      <td align="right">密码</td> 
  58.  
  59.      <td><input type="password" name="password" id="" value="<?php echo $row['password']?>">密码不能为空</td> 
  60.  
  61.     </tr> 
  62.  
  63.     <tr> 
  64.  
  65.      <td align="right">邮箱</td> 
  66.  
  67.      <td><input type="text" name="email" id="" value="<?php echo $row['email']?>"></td> 
  68.  
  69.     </tr> 
  70.  
  71.     <tr> 
  72.  
  73.      <td align="right">性别</td> 
  74.  
  75.      <td> 
  76.  
  77.       <input type="radio" name="sex" id="" value="1" <?php="" if($row['sex']="='1'){" echo="" 'checked';}?="">/>男 
  78.  
  79.       <input type="radio" name="sex" id="" value="2" <?php="" if($row['sex']="='2'){" echo="" 'checked';}?="">/>女 
  80.  
  81.       <input type="radio" name="sex" id="" value="3" <?php="" if($row['sex']="='3'){" echo="" 'checked';}?="">/>保密 
  82.  
  83.      </td> 
  84.  
  85.     </tr> 
  86.  
  87.     <tr> 
  88.  
  89.      <td align="right">个人简介</td> 
  90.  
  91.      <td> 
  92.  
  93.       <textarea name="txt" id="" cols="50" rows="10"><?php echo $row['txt']?></textarea> 
  94.  
  95.      </td> 
  96.  
  97.     </tr> 
  98.  
  99.     <tr> 
  100.  
  101.      <td colspan="2"><input type="submit" name="act" value="修改"></td> 
  102.  
  103.     </tr> 
  104.  
  105.    </tbody></table> 
  106.  
  107.   </form> 
  108.  
  109. </div> 
  110.  
  111. //将修改的信息存入数据库 
  112.  
  113. <?php 
  114.  
  115. header("Content-type:text/html; charset=utf-8"); 
  116.  
  117. //通过post获取页面提交数据信息 
  118.  
  119. $data = $_POST; 
  120.  
  121. //print_r($data); 
  122.  
  123. include_once "connect.php"; 
  124.  
  125. $sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'"; 
  126.  
  127. echo $sql; 
  128.  
  129. $res = mysql_query($sql,$link); 
  130.  
  131. if($res){ 
  132.  
  133.  header("Location:hello.php"); 
  134.  
  135.  //echo "alert('修改成功')"; 
  136.  
  137. }else{ 
  138.  
  139.  header("Location:update1.php?id=".$data['id']); 
  140.  
  141.  //echo "alert('修改失败')"; 
  142.  
  143. } 
  144.  
  145. ?> 

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.  
  23. ?> 

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

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