老师,为啥我这个注册不了用户,错在哪了
来源:1-1 项目与数据库创建
I编程
2018-04-25 12:54:36
html与php代码
<!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 rel="stylesheet" href="css/login.css" />
<?php
if(!empty($_POST['username']))
{
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$repassword = trim($_POST['repassword']);
if(!$username)
{
echo '用户名不能为空';
}
if(!$password)
{
echo '密码不能为空';
}
$con=mysql_connect('127.0.0.1','root','root');
if(!$con){
echo mysql_error();exit;
}
mysql_select_db('hr_yh');
mysql_set_charset('utf-8');
$sql = "INSERT `hr_user`(`username`,`password`,`createtime`) values('{$username}','{$password}','{$_SERVER['REQUEST_TIME']}')";
$obj=mysql_query($sql);
if($obj){
$userId = mysql_insert_id();
echo sprintf('恭喜您注册成功,用户名是:%s,用户id:%s', $username, $userId); exit
}else{
echo mysql_error();exit;
}
}
?>
</head>
<body>
<div class="warp">
<div class="top">弘润裁床管理系统</div>
<div class="top-a"></div><!--login图-->
<div class="user">
<div class="user-title">
<p>用户注册 </p>
</div>
<form class="login-table" action="register.html" method="post">
<div class="login-left">
<label class="username">用户名:</label>
<input type="text" class="yhmiput" name="username" placeholder="请输入用户名"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-right">
<label class="passwd">密码:</label>
<input type="password" class="yhmiput" name="password" placeholder="请输入密码"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
</form>
<div class="login-btn"> <button type="submit">成功注册</button></div>
<script src="js/login.js"></script>
</div>
</html>
}
?>
</head>
<body>
<div class="warp">
<div class="top">弘润裁床管理系统</div>
<div class="top-a"></div><!--login图-->
<div class="user">
<div class="user-title">
<p>用户注册 </p>
</div>
<form class="login-table" action="register.html" method="post">
<div class="login-left">
<label class="username">用户名:</label>
<input type="text" class="yhmiput" name="username" placeholder="请输入用户名"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-right">
<label class="passwd">密码:</label>
<input type="password" class="yhmiput" name="password" placeholder="请输入密码"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
</form>
<div class="login-btn"> <button type="submit">成功注册</button></div>
<script src="js/login.js"></script>
</div>
</html>

4回答
你好,建议参考代码:
<?php
if(!empty($_POST['username']))
{
$username = trim($_POST['username']);
$password = trim($_POST['password']);
//$repassword = trim($_POST['repassword']);
if(!$username)
{
echo '用户名不能为空';
}
if(!$password)
{
echo '密码不能为空';
}
$con=mysql_connect('127.0.0.1','root','root');
if(!$con){
echo mysql_error();exit;
}
mysql_select_db('hr_yh');
$sql="SELECT COUNT(`id`)as tottal FROM `hr_user` WHERE `username`='{$username}'";
$obj=mysql_query($sql);
$ren=mysql_fetch_assoc($obj); //结果数据又不能传入数据库了。怎么解决 //验证用户是否存在
if(isset($ren['tottal'])&&$ren['tottal']>0){
echo '用户名已存在,请重新输入';exit;
} //密码加密处理
else{
$sql = "INSERT `hr_user`(`username`,`password`,`createtime`) values('{$username}','{$password}','{$_SERVER['REQUEST_TIME']}')";
$obj=mysql_query($sql);
if($obj){
$userId = mysql_insert_id();
echo sprintf('恭喜您注册成功,用户名是:%s,用户id:%s', $username, $userId);
exit;
}
}
// if(!$password){
// return false;
// }
// return md5(md5($password).'hr');
// unset($obj, $sql);
}
?>
<!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 rel="stylesheet" href="css/login.css" />
</head>
<body>
<div class="warp">
<div class="top">弘润裁床管理系统</div>
<div class="top-a"></div><!--login图-->
<div class="user">
<div class="user-title">
<p>用户注册 </p>
</div>
<form class="login-table" action="register.php" method="post">
<div class="login-left">
<label class="username">用户名:</label>
<input type="text" class="yhmiput" name="username" placeholder="请输入用户名"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-right">
<label class="passwd">密码:</label>
<input type="password" class="yhmiput" name="password" placeholder="请输入密码"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-btn">
<button type="submit">成功注册</button>
</div>
</form>
<script src="js/login.js"></script>
<body>建议学生按照代码规范贴出代码,祝学习愉快!
guly
2018-04-26
你好,
你好,密码加密在插入数据库之前处理即可,参考代码如下:
if(!$password){
return false;
}else{
$password= md5(md5($password).'hr');
}祝学习愉快!
guly
2018-04-25
你好,从您贴出的代码可以看出sql语句有误
$sql="SELECT COUNT('id')as tottal FROM 'hr_user' WHERE 'username'='{username}'";
1、 字段名称不应该用单引号 应该用tab键上面的符号,
2、变零值书写有误应该为$username,
参考代码如下:
$sql="SELECT COUNT(`id`)as tottal FROM `hr_user` WHERE `username`='{$username}'";
如果解决您的问题请采纳,祝学习愉快!
guly
2018-04-25
你好,1、 PHP与html混编文件应以.php为后缀名
PHP处理文件与html文件分开
2、form属性 包函提交属性按钮,
正确的为:

同时不要忘记修改form中action
<form class="login-table" action=register.php" method="post">
参考代码如下:
<?php
if(!empty($_POST['username']))
{
$username = trim($_POST['username']);
$password = trim($_POST['password']);
//$repassword = trim($_POST['repassword']);
if(!$username)
{
echo '用户名不能为空';
}
if(!$password)
{
echo '密码不能为空';
}
$con=mysql_connect('127.0.0.1','root','root');
if(!$con){
echo mysql_error();exit;
}
mysql_select_db('hr_yh');
mysql_set_charset('utf-8');
$sql = "INSERT `hr_user`(`username`,`password`,`createtime`) values('{$username}','{$password}','{$_SERVER['REQUEST_TIME']}')";
$obj=mysql_query($sql);
if($obj){
$userId = mysql_insert_id();
echo sprintf('恭喜您注册成功,用户名是:%s,用户id:%s', $username, $userId);
exit;
}
else{
echo mysql_error();exit;
}
}
?>
<!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 rel="stylesheet" href="css/login.css" />
</head>
<body>
<div class="warp">
<div class="top">弘润裁床管理系统</div>
<div class="top-a"></div><!--login图-->
<div class="user">
<div class="user-title">
<p>用户注册 </p>
</div>
<form class="login-table" action="register.php" method="post">
<div class="login-left">
<label class="username">用户名:</label>
<input type="text" class="yhmiput" name="username" placeholder="请输入用户名"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-right">
<label class="passwd">密码:</label>
<input type="password" class="yhmiput" name="password" placeholder="请输入密码"><p class="zre">5到15个字符,一个汉字为两个字符</p>
</div>
<div class="login-btn">
<button type="submit">成功注册</button>
</div>
</form>
<script src="js/login.js"></script>
<body>如果解决您的问题请采纳,祝学习愉快!
相似问题