PHP+mysql 入门级通讯录(五)

这篇博客介绍了如何在PHP+MySQL环境中实现用户注册功能。通过register.php和Reg.php文件,用户输入信息并提交,然后在detail.php中查询登录用户的信息。修改用户信息的流程与之前的联系人管理类似,涉及Modde.php和moddet.php两个文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

七、用户注册

同样的,有用户登录就一定会有用户注册
新建文件register.php,代码如下:

<!-- 注册账户 html -->
<!-- 博主邮箱1161872658@qq.com  有问题可以联系我,欢迎任何意见和建议 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title>用户注册</title>
</head>
<body>
    <div align="right">  
    <p>当前版本:V1.0</p>
        [<a href="login.php">回到首页</a>]  
    </div>  
    <h2 align="center">用户注册</h2>
        <form id="form4" name="form4" method="post" action="reg.php">
      <table width="400" border="1" align="center" bordercolor="#DDDDDD">  
        <tr>  
          <td width="100">用户名:</td>  
          <td width="300"><label>  
            <input name="uname" type="text" id="uname" size="35" />  
          </label></td>  
        </tr>  
          <tr>  
          <td width="100">密码:</td>  
          <td width="300"><label>  
            <input name="pwd" type="password" id="pwd" size="35" />  
          </label></td>  
        </tr>  
 <tr>  
          <td colspan="2"><label>  
            <div align="right">  
              <input type="submit" name="Submit6" value="提交" />  

          </div>        </label></td>  
        </tr>  

        </form>
</body>
</html>

新建文件Reg.php

<!-- 注册账户  PHP -->
    <?php  
    $uname = $_POST['uname']; 
    $pwd=$_POST['pwd'];
 $sql="select * from users where uname='$uname' and pwd='$pwd'";
      require('conn.php'); 
       $row = MySQL_fetch_row($result);

if (($row[0]!=$uname)&&($row[1]!=$pwd)) { //此处并没有仔细研究,慎写 
//若查询结果与所添加项不相同,则执行添加操作
    $sql = "INSERT INTO `mydb`.`users` (`uname` , `pwd`) VALUES ('$uname','$pwd');"; 
    require('conn.php'); 

    if($result)  
    {  
        //在users表中添加数据后,需同步向detail(用户详情表)添加uname的记录(方便后续对用户详细信息的修改等操作)
        $sql="insert into `mydb`.`detail` (`uname`) VALUES ('$uname');";
        require('conn.php');
   ?>
     <script type="text/javascript">  
        alert("添加成功!");  
        window.location.href="login.php";
    </script> 

    <?php
     }  
       else{
        ?>
            <script type="text/javascript">  
        alert("添加失败!");  
        window.location.href="register.php";
    </script>
    <?php
    }

}
else {
?>
            <script type="text/javascript">  
        alert("该用户已存在!请重新注册或进行密码修改");  
        window.location.href="register.php";
    </script>
    <?php
    }
    ?> */

其实以上操作跟添加联系人是一样的,就是在register页面输入信息,然后通过reg.php对数据库执行操作
注册页面写完之后,在Index页面将div修改成如下,然后新建一个文件叫detail.php,用于查询当前登录人的信息

    <div align="right">  
         <a href="detail.php" > 
         <?php session_start();
    $s=$_SESSION['user'];
?> </a>
<br/>
        [<a href="input.php">添加</a>]  
    </div> 

detail.php代码如下:

<!-- 当前用户详细信息 -->
<!DOCTYPE html>
<html>
<head>
    <title>当前用户详细信息</title>
</head>
<body>
<meta charset="utf-8">
<div align="right"> [<a href="index.php">回到首页</a>] <br/>[<a href="login.php">切换账号</a>] </div>
    <?php  
    session_start();//调用session
$uname=$_SESSION['user'];//去除session里的user数据
    $sql = "select * from detail WHERE uname='$uname' ";  
    require('conn.php');  //调用conn执行sql语句

    $row = MySQL_fetch_row($result);//将获取的结果以row的形式赋给$row
// 根据从数据库查询结果  将数据绑定到表单显示出来  
    ?>
<h2 align="center">当前登录账户信息</h2>
<form>
  <table width="381" border="1" align="center" bordercolor="#DDDDDD">  
        <tr>  
          <td width="64" >姓名</td>  
          <td width="307"><label> 
            <input name="uname" type="text" id="uname" size="30"  disabled="disabled"  value=<?php  echo $_SESSION['user']; ?>  >
          </label></td>
        </tr>
          <tr>  
          <td width="64" >电话</td>  
          <td width="307"><label> 
            <input name="mobile" type="text" id="mobile" size="30" disabled="disabled" value=<?php echo $row[1];?> >
          </label></td>  
                      <tr> 
            <tr>  
          <td width="64" >生日</td>  
          <td width="307"><label> 
            <input name="birthday" type="text" id="birthday" size="30" disabled="disabled" value=<?php echo $row[2];?> >
          </label></td>  
                      <tr>  
          <td width="64" >QQ</td>  
          <td width="307"><label> 
            <input name="qq" type="text" id="qq" size="30" disabled="disabled" value=<?php echo $row[3];?>>
          </label></td> 
        </tr>
                              <tr>  
          <td width="64" >Email</td>  
          <td width="307"><label> 
            <input name="email" type="text" id="email" size="30" disabled="disabled" value=<?php echo $row[4];?> >
          </label></td> 
        </tr>
                              <tr>  
          <td width="64" >地址</td>  
          <td width="307"><label> 
            <input name="addr" type="text" id="addr" size="30"disabled="disabled" value=<?php echo $row[5];?>  >
          </label></td> 
        </tr>
                      <tr>  
          <td width="64" >备注</td>  
          <td width="307"><label> 
            <input name="other" type="text" id="other" size="30"disabled="disabled" value=<?php echo $row[6];?>  >
          </label></td> 
        </tr>
        <tr>
         <td align="right" colspan="2"><a href="Modde.php?id=<?php echo $row[0];?>" >编辑用户信息</a>      
        </td> 
        </tr>
  </table>
</form>
</body>
</html>

点击编辑用户信息,跳转到Modde.php页(名字起得有点多了)
新建Modde.php

<!-- 修改用户详细信息 html -->
<!DOCTYPE html>
<html>
<head>
    <title>用户信息编辑</title>
</head>
<body>
<meta charset="utf-8">
<div align="right"> [<a href="index.php">回到首页</a>] 
<br/>
[<a href="login.php">切换账号</a>] 
</div>
    <?php  
    session_start();
$uname=$_SESSION['user'];//调用session  取出存在里面的user
    $sql = "select * from detail WHERE uname='$uname' ";  
    require('conn.php'); 

    $row = MySQL_fetch_row($result);

    ?>
<h2 align="center">当前登录账户信息</h2>
        <form id="form0" name="form0" method="post" action="moddet.php">
  <table width="381" border="1" align="center" bordercolor="#DDDDDD">  
        <tr>  
          <td width="64" >姓名</td>  
          <td width="307"><label> 
            <input name="uname" type="text" id="uname" size="30"    value=<?php echo $_SESSION['user'];?> >
          </label></td>  

        </tr>
          <tr>  
          <td width="64" >电话</td>  
          <td width="307"><label> 
            <input name="mobile" type="text" id="mobile" size="30"  value=<?php echo $row[1];?> >
          </label></td>  
                      <tr> 
            <tr>  
          <td width="64" >生日</td>  
          <td width="307"><label> 
            <input name="birthday" type="date" id="birthday" size="30"  value=<?php echo $row[2];?> >
          </label></td>  
                      <tr>  
          <td width="64" >QQ</td>  
          <td width="307"><label> 
            <input name="qq" type="text" id="qq" size="30"  value=<?php echo $row[3];?>>
          </label></td> 
        </tr>
                              <tr>  
          <td width="64" >Email</td>  
          <td width="307"><label> 
            <input name="email" type="text" id="email" size="30"  value=<?php echo $row[4];?> >
          </label></td> 
        </tr>
                              <tr>  
          <td width="64" >地址</td>  
          <td width="307"><label> 
            <input name="addr" type="text" id="addr" size="30" value=<?php echo $row[5];?>  >
          </label></td> 
        </tr>
                      <tr>  
          <td width="64" >备注</td>  
          <td width="307"><label> 
            <input name="other" type="text" id="other" size="30" value=<?php echo $row[6];?>  >
          </label></td> 
        </tr>
        <tr>  
          <td colspan="2"><label>  
            <div align="right">  
              <input type="submit" name="Submit7" value="提交" />  
              <input type="reset" name="button" value="清空" />  
          </div>        </label></td>  
        </tr> 
        </tr>
  </table>
</form>
</body>
</html>

跟修改联系人也是一样的
新建一个moddet.php用于处理修改用户信息的操作

<meta charset="utf-8"> 
<!-- 修改用户详细信息 PHP -->
    <?php  
    session_start();
    $uname = $_SESSION['user'] ;
    $mobile = $_POST['mobile'];
    $birthday = $_POST['birthday'];  
    $qq = $_POST['qq'];  
    $email = $_POST['email'];  
    $addr = $_POST['addr'];  
    $other=$_POST['other'];

    $sql = "update  mydb.detail 
    set mobile='$mobile',birthday='$birthday',qq='$qq',email='$email', addr='$addr',other='$other' where uname='$uname';";

    require('conn.php'); 
    if($result)  
    {  
   ?>
     <script type="text/javascript">  
        alert("修改成功!");  
        window.location.href="modde.php";
    </script> 
    <?php
    }  
    else{
      ?>s
          <script type="text/javascript">  
        alert("修改失败!");  
        window.location.href="modde.php";
    </script>
    <?php
    }
    ?>  

因为关于用户的操作跟之前所说的联系人的相关操作代码处理是一样的,只是做了针对性的修改,如果有不懂的,可以参见前面的代码部分。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值