首先先做一个登陆页面,验证对错不是关键,首要任务是利用Cookie传递用户的name以判断修改的个人信息是谁。
<?php
/** file:login.inc.php 登录系统的主控制文件和主入口文件*/
/*声明一个删除Cookie的函数,调用时消除在客户端设置的所以Cookie*/
function clearCookies(){
//只指定Cookie识别名称一个参数,即删除客户端中这个指定名称的Cookie资料
//生存空间默认为空,则生存期限与浏览器一样,浏览器关闭时Cookie就会被删除
setcookie('name');
setcookie('isLogin');
}
include "conn/conn.php";
@$name = htmlspecialchars($_POST['name']);
@$pwd = $_POST['pwd'];
$pwd = md5($pwd);
/*如果用户的操作是提交表单,则条件成立*/
if(isset($_POST['submit'])){
clearCookies();
$sql = "SELECT * FROM tb_grnews WHERE name = '$name' and pwd='$pwd'";
$res = mysqli_query($sql,$conn);
/*mysql_num_rows只能用于select语句*/
$rows=mysqli_num_rows($res);
if($rows>=0){
/*向Cookie中设置标识符为name,值是表单中提交的,期限为一周*/
setcookie('name',$name/*如果不设置时间,默认session,浏览器关闭后失效,time()+60*/);
/*向Cookie中设置标识符为isLogin,用来在其它页面检查用户是否登录*/
setcookie('isLogin','1'/*如果不设置时间,默认session,浏览器关闭后失效>,time()+60*60*24*7*/);
/*如果Cookie设置成功则转向网站首页*/
header("Location:pi.php");//跳转页面,注意路径
}else{
echo '<font color="red">用户名或密码错误!</font>';
}
}else if(@$_GET["action"]=="logout"){
//退出时消除在客户端设置的所有Cookie
clearCookies();
}
?>
<html>
<head>
<title>登录系统</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="login.css" />
</head>
<body>
<div class="content">
<!-- 登录面板 -->
<div class="panel">
<form action="login.inc.php" method="post">
<div class="group">
<label for="name">用户名:</label>
<input id="name" type="text" name="name" placeholder="请输入账号" />
</div>
<div class="group">
<label for="pass">密码:</label>
<input id="pwd" type="password" name="pwd" placeholder="请输入密码" />
</div>
<div class="login">
<button type="submit" name="submit">登录</button>
</div>
<div class="register">
<input name="" type="reset" value="重置">
</div>
</form>
</div>
</body>
</html>
然后便是展示个人信息的pi.php
<?php /** file:login.inc.php 登录系统的首页*/
/*如果用户没有通过身份验证,页面跳转至登录页面*/
if(!(isset($_COOKIE['isLogin'])&&$_COOKIE['isLogin']=='1')){
header("Location:login.inc.php");
exit;
}
$name=$_COOKIE['name'];
/*if(!(isset($_POST['name']))){
header("Location:dengru.php");
exit;
} */
header("Content-Type:text/html;charset=utf-8");
include"conn/conn.php";
$sqlstr0 = "SELECT pwd FROM tb_grnews WHERE name = $name";
$result0 = mysqli_query($conn,$sqlstr0);
$pwd=mysqli_fetch_array($result0);
$sqlstr = "SELECT signature FROM tb_grnews WHERE name = $name";
$result = mysqli_query($conn,$sqlstr);
$signature=mysqli_fetch_array($result);
$sqlstr2 = "SELECT email FROM tb_grnews WHERE name = $name";
$result2 = mysqli_query($conn,$sqlstr2);
$email=mysqli_fetch_array($result2);
$sqlstr3 = "SELECT tel FROM tb_grnews WHERE name = $name";
$result3 = mysqli_query($conn,$sqlstr3);
$tel=mysqli_fetch_array($result3);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/index1.css" rel="stylesheet" type="text/css" />
<style>
.div-c{ float:left;width:15%;border:0px solid; text-align:center; margin-left:20%; }
.div-d{ float:right;width:50%;border:0px solid #000;
margin-right:15%; }
</style>
</head>
<body background="images/9402.jpg">
<div class="box">
<div class="top">
<div class="top-imgandsousuo"><div class="top-img"><a href="#" class="top-link">插入图片</a></div>
<div class="top-sousuo"><a href="#">搜索框</a></div></div>
<div class="top-b"><table width="400" border="0">
<tr>
<td><a href="#" class="top-link">🏠首页</a></td>
<td><a href="#" class="top-link">⭕论坛系统</a></td>
<td><a href="#" class="top-link">🗨账号系统</a></td>
<td><a href="#" class="top-link">📚学习系统</a></td>
<td><span><a href="#" class="top-link">登录</span>|<span><a href="#" class="top-link">注册</a></span></td>
</tr>
</table>
</div>
</div>
</div>
<div class="div-c"><table id="table2" width="88%" border="2" cellspacing="0" cellpadding="2" align="center" height="400px" bgcolor="#FFFFFF" background="images/9401.jpg">
<tr>
<td align="center"><a href="pi.php">个人信息</a></td>
</tr>
<tr>
<td align="center"><a href="imagesup.php">头像</a></td>
</tr>
</table>
</div>
<div class="div-d">
<table id="table2" width="100%" border="2" cellspacing="15" cellpadding="2" align="center" height="400px" bgcolor="#FFFFFF" background="images/9401.jpg">
<form id="searchform2" name="searchform2" method="post" action="index.php">
<tr>
<td colspan="3" align="left" height="5%">
<?php echo $name.'的个人信息表';?>
<hr align="center" width="100%" size="4" noshade="noshade" id="hr" color="#F00">
</hr></td>
<td><input type='hidden' name='name' id="name" value="<?php echo $name ?>"><input name="tijiao" type="submit" class="button" value="修改" />
</td>
</tr>
<tr>
<td width="-5" height="25"> </td>
<td width="35%" align="right">账户: </td>
<td width="194" class="text" height="30"><input name="name" id="name" type="text" class="text" size="20" placeholder="<?php echo $name ?>" disabled="" /></td>
<td width="-4"> </td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">密码: </td>
<td align="left"><input name="pwd" id="pwd" type="text" class="text" size="20" placeholder="<?php echo $pwd[0] ?>" disabled=""/></td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">签名: </td>
<td align="left"><input name="signature" type="text" class="text" placeholder="<?php echo $signature[0] ?>" disabled=""/></td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">邮箱: </td>
<td align="left"><input name="email" id="email" class="text" placeholder="<?php echo $email[0] ?>" disabled=""></td>
<td> </td>
</tr>
<tr>
<td height="25"></td>
<td align="right">电话: </td>
<td align="left">
<label for="tel"></label>
<input name="tel" type="text" class="text" id="tel" placeholder="<?php echo $tel[0] ?>" disabled=""/>
</td>
<td width="-4"></td>
</tr>
<tr>
<td height="25"> </td>
<td colspan="2" align="center">
</td>
<td> </td>
</tr> </form>
</table>
</div>
</body>
</html>
通过点击修改跳转到index.php的修改个人信息网页
<?php /** file:login.inc.php 登录系统的首页*/
/*如果用户没有通过身份验证,页面跳转至登录页面*/
if(!(isset($_COOKIE['isLogin'])&&$_COOKIE['isLogin']=='1')){
header("Location:login.inc.php");
exit;
}
$name=$_COOKIE['name'];
header("Content-Type:text/html;charset=utf-8");
include"conn/conn.php";
$sqlstr0 = "SELECT pwd FROM tb_grnews WHERE name = $name";
$result0 = mysqli_query($conn,$sqlstr0);
$pwd=mysqli_fetch_array($result0);
$sqlstr = "SELECT signature FROM tb_grnews WHERE name = $name";
$result = mysqli_query($conn,$sqlstr);
$signature=mysqli_fetch_array($result);
$sqlstr2 = "SELECT email FROM tb_grnews WHERE name = $name";
$result2 = mysqli_query($conn,$sqlstr2);
$email=mysqli_fetch_array($result2);
$sqlstr3 = "SELECT tel FROM tb_grnews WHERE name = $name";
$result3 = mysqli_query($conn,$sqlstr3);
$tel=mysqli_fetch_array($result3);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/index1.css" rel="stylesheet" type="text/css" />
<style>
.div-c{ float:left;width:15%;border:0px solid; text-align:center; margin-left:20%; }
.div-d{ float:right;width:50%;border:0px solid #000;
margin-right:15%; }
</style>
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script>
function cf(){
var x;
x=form1.pwd.value;
if(x==""){
alert("密码不能为空");
form1.pwd.focus();
return false;
}else if(x.length<6||x.length>12){
alert("请输入长度在6-12之内的密码");
form1.pwd.focus();
return false;
}else{
return true;
}}
/分割线/
</script>
</head>
<body background="images/9402.jpg">
<div class="box">
<div class="top">
<div class="top-imgandsousuo"><div class="top-img"><a href="#" class="top-link">插入图片</a></div>
<div class="top-sousuo"><a href="#">搜索框</a></div></div>
<div class="top-b"><table width="400" border="0">
<tr>
<td><a href="#" class="top-link">🏠首页</a></td>
<td><a href="#" class="top-link">⭕论坛系统</a></td>
<td><a href="#" class="top-link">🗨账号系统</a></td>
<td><a href="#" class="top-link">📚学习系统</a></td>
<td><span><a href="#" class="top-link">登录</span>|<span><a href="#" class="top-link">注册</a></span></td>
</tr>
</table>
</div>
</div>
</div>
<div class="div-c"><table id="table2" width="88%" border="2" cellspacing="0" cellpadding="2" align="center" height="400px" bgcolor="#FFFFFF" background="images/9401.jpg">
<tr>
<td align="center"><a href="pi.php">个人信息</a></td>
</tr>
<tr>
<td align="center"><a href="imagesup.php">头像</a></td>
</tr>
</table>
</div>
<div class="div-d">
<table id="table2" width="100%" border="2" cellspacing="15" cellpadding="2" align="center" height="400px" bgcolor="#FFFFFF" background="images/9401.jpg">
<form id="form1" name="intFrom" method="post" action="index_update_ok.php" onsubmit="return cf()">
<tr>
<td colspan="4" align="left" height="5%"> 修改信息表
<hr align="center" width="100%" size="4" noshade="noshade" id="hr" color="#F00">
</hr></td>
</tr>
<tr>
<td width="-5" height="25"> </td>
<td width="13%" align="right">账户: </td>
<td width="194" class="text" height="30"><input name="name" id="name" type="text" class="text" size="20" placeholder="<?php echo $name ?>" disabled="" /></td>
<td width="-4"> </td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">密码: </td>
<td align="left"><input name="pwd" id="pwd" type="text" class="text" size="20" value="<?php echo $pwd[0] ?>"></td>
<td><font color="#FF0000" size="-5">*请输入6-12位的密码*</font></td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">签名: </td>
<td align="left"><input name="signature" id="signature" type="text" class="text" value="<?php echo $signature[0] ?>" /></td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td align="right">邮箱: </td>
<td align="left"><input name="email" id="email" class="text" value="<?php echo $email[0] ?>" /></td>
<td> </td>
</tr>
<tr>
<td height="25"></td>
<td align="right">电话: </td>
<td align="left">
<label for="tel"></label>
<input name="tel" id="tel" type="text" class="text" value="<?php echo $tel[0] ?>" />
</td>
<td width="-4"><font color="#FF0000" size="-5">*请输入11位的电话*</font></td>
</tr>
<tr>
<td height="25"></td>
<td colspan="2" align="center">
<input type="hidden" name="action" value="update">
<input type='hidden' name='name' id="name" value="<?php echo $name ?>">
<input name="submit" id="submit" type="submit" class="button" value="保存"/><input type="reset" class="button" value="重置" />
</td>
<td> </td>
</tr> </form>
</table>
</div>
</body>
</html>
最后通过表单提交到index_update_ok.php进行更新数据库操作
<?php
header("Content-type:text/html;charset=utf-8");
include_once("conn/conn.php");
if($_POST['action'] == "update"){
$name =iconv('UTF-8','gb2312',$_POST['name']); //获取Ajax传递的值,并实现字符编码转换
$pwd = iconv('UTF-8','gb2312',$_POST['pwd']); //获取Ajax传递的值,并实现字符编码转换
$signature = iconv('UTF-8','gb2312',$_POST['signature']);
$email =iconv('UTF-8','gb2312',$_POST['email']);
$tel =iconv('UTF-8','gb2312',$_POST['tel']);
$sqlstr ="UPDATE tb_grnews SET pwd='$pwd', signature='$signature',email='$email',tel='$tel' WHERE name='$name'";
//定义更新语句
$result = mysqli_query($conn,$sqlstr);//执行更新语句
if($result){
/*$username = $_POST['name'];
$home_url = 'pi.php?user=$username;
header('Location:'.$home_url);*/
header('Location:pi.php');
}else{
echo "修改失败.<br>$sqlstr.<br>";
echo mysqli_error($conn);
}
}
/*if (isset($_POST["submit"]) && $_POST["submit"] == "保存") {
include "conn/conn.php";
//require_once('connect_mysql.php');
/*$pwd = $_POST["pwd"];
$signature = $_POST["signature"];
$email = $_POST["email"];
$tel = $_POST["tel"];*/
/*$pwd = iconv('UTF-8','gb2312',$_POST['pwd']); //获取Ajax传递的值,并实现字符编码转换
$signature = iconv('UTF-8','gb2312',$_POST['signature']);
$email =iconv('UTF-8','gb2312',$_POST['email']);
$tel =iconv('UTF-8','gb2312',$_POST['tel']);
$sql4 = "update tb_grnews SET pwd='{$pwd}',signature= '{$signature}',email='{$email}',tel='{$tel}' WHERE name = '{$name}'";
$result4 = mysqli_query($conn, $sql4);
echo "<script> alert('修改成功!'); </script>";
}*/
/* if(isset($_POST['submit1'])){
include"conn/conn.php";
$sql_1 = "UPDATE tb_grnews SET pwd='".$pwd[0]."' WHERE name = '$name'";
$sql_2 = "UPDATE tb_grnews SET signature='".$signature[0]."' WHERE name = '$name'";
$sql_3 = "UPDATE tb_grnews SET email='".$email[0]."' WHERE name = '$name'";
$sql_4 = "UPDATE tb_grnews SET tel='".$tel[0]."' WHERE name = '$name'";
$result_1=mysqli_query($conn,$sql_1);
$result_2=mysqli_query($conn,$sql_2);
$result_3=mysqli_query($conn,$sql_3);
$result_4=mysqli_query($conn,$sql_4);
如果INSERT语句执行成功,并对数据表userdata有行数影响,则插入成功
if($result_1||$result_2||$result_3||$result_4){
echo "<script> alert('修改成功!'); </script>";
echo "<meta http-equiv='Refresh' content='0;URL=$url'>";
}else{
echo "<script> alert('修改失败!'); </script>";
}
}
mysqli_close($conn);*/
?>
PS:记得要在同个文件夹中建立一个conn子文件里面放conn.php
<?php
$conn=mysqli_connect("localhost","root","111","1024") or die("数据库连接失败".mysql_error());
mysqli_query($conn,"set names gb2312");
?>