老师,我这个运行只会闪一下静态页面
<?php //表单进行了提交处理 if(!empty($_post['username'])) { include_once './lib/fun.php'; $username = trim($_post('username'));//mysql_real_escape_string()进行过滤,检测用户输入的是否NULL $password = trim($_post('password')); $repassword = trim($_post('repassword')); //判断用户名不能为空 if(!$username){ echo "用户名不能为空";exit; } if(!$password){ echo "密码不能为空";exit; } if(!$repassword){ echo "确认密码不能为空";exit; } if($password !== $repassword){ echo "两次密码输入不一致"; exit; } //数据库操作 $con = mysqlInit('127.0.0.1','root','','dshang'); if(!$con) { echo "mysql_error()"; exit; } //判断用户是否在用户表存在 $sql= "SELECT count('id') as total FROM `d_user` WHERE 'username' = '{$username}' "; $obj = mysql_query($sql); $result = mysql_fetch_assoc($obj); var_dump($result); die; } ?>
这里是封装函数
<?php /** * 数据库连接初始化 * @param $host * @param $username * @param $password * @param $dbName * @return bool|resource */ function mysqlInit($host, $username, $password, $dbName) { $con = mysql_connect($host, $username,$password); if (!$con) { return false; } mysql_select_db($dbName); mysql_query($con,'set names utf8'); return $con; }
正在回答
您好,接收post方式的数据应该使用$_POST['name']。请注意$_POST大写,并且为[]。祝学习愉快!
<?php
//表单进行了提交处理
if(!empty($_post['username']))
{
include_once './lib/fun.php';
$username = trim($_post('username'));//mysql_real_escape_string()进行过滤,检测用户输入的是否NULL
$password = trim($_post('password'));
$repassword = trim($_post('repassword'));
//判断用户名不能为空
if(!$username){
echo "用户名不能为空";exit;
}
if(!$password){
echo "密码不能为空";exit;
}
if(!$repassword){
echo "确认密码不能为空";exit;
}
if($password !== $repassword){
echo "两次密码输入不一致";
exit;
}
//数据库操作
$con = mysqlInit('127.0.0.1','root','','dshang');
if(!$con)
{
echo "mysql_error()";
exit;
}
//验证用户名是否存在
$sql= "SELECT count('id') as total
FROM `dsuser` WHERE 'username' = '{$username}' ";
$obj = mysql_query($sql);
$result = mysql_fetch_assoc($obj);
var_dump($result); die;
if(isset($result['total'])&& $result['total']>0)
{
echo "用户名已存在,请重新输入";
}
//密码加密处理
$password= createpassword ($password);
unset($obj,$result,$sql);
//插入数据
$sql= "INSERT 'duser' ('username,password,create_time') values('{$username}','{$password}','{$_server['request_time']}') ";
$obj=mysql_query($sql);
if($obj)
{
$userid = mysql_insert_id();//插入成功的主键id
echo sprintf('恭喜您注册成功,用户名是:%s,用户id:%s',$username,$userid);
exit;
}
else
{
echo mysql_error(); exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>M-GALLARY|用户注册</title>
<link type="text/css" rel="stylesheet" href="./static/css/common.css">
<link type="text/css" rel="stylesheet" href="./static/css/add.css">
<link rel="stylesheet" type="text/css" href="./static/css/login.css">
</head>
<body>
<div class="header">
<div class="logo f1">
<img src="./static/image/logo.png">
</div>
<div class="auth fr">
<ul>
<li><a href="login.php">登录</a></li>
<li><a href="register.php">注册</a></li>
</ul>
</div>
</div>
<div class="content">
<div class="center">
<div class="center-login">
<div class="login-banner">
<a href="#"><img src="./static/image/login_banner.png" alt=""></a>
</div>
<div class="user-login">
<div class="user-box">
<div class="user-title">
<p>用户注册</p>
</div>
<form class="login-table" name="register" id="register-form" action="register.php" method="post">
<div class="login-left">
<label class="username">用户名</label>
<input type="text" class="yhmiput" name="username" placeholder="Username" id="username">
</div>
<div class="login-right">
<label class="passwd">密码</label>
<input type="password" class="yhmiput" name="password" placeholder="Password" id="password">
</div>
<div class="login-right">
<label class="passwd">确认</label>
<input type="password" class="yhmiput" name="repassword" placeholder="Repassword"
id="repassword">
</div>
<div class="login-btn">
<button type="submit">注册</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p><span>M-GALLARY</span> ©2017 POWERED BY IMOOC.INC</p>
</div>
</body>
<script src="./static/js/jquery-1.10.2.min.js"></script>
<script src="./static/js/layer/layer.js"></script>
<script>
$(function () {
$('#register-form').submit(function () {
var username = $('#username').val(),
password = $('#password').val(),
repassword = $('#repassword').val();
if (username == '' || username.length <= 0) {
layer.tips('用户名不能为空', '#username', {time: 2000, tips: 2});
$('#username').focus();
return false;
}
if (password == '' || password.length <= 0) {
layer.tips('密码不能为空', '#password', {time: 2000, tips: 2});
$('#password').focus();
return false;
}
if (repassword == '' || repassword.length <= 0 || (password != repassword)) {
layer.tips('两次密码输入不一致', '#repassword', {time: 2000, tips: 2});
$('#repassword').focus();
return false;
}
return true;
})
})
</script>
</html>这是register.php的代码部分
数据库结构有id,username,password,create_time
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星