永发信息网

跪求各位php高手,为什么不能判断用户名重复,不知道哪里错了??

答案:2  悬赏:40  手机版
解决时间 2021-02-01 12:22
  • 提问者网友:练爱
  • 2021-01-31 19:58
<?php
header("Content-Type:text/html;charset=utf8");
$local="localhost";
$root="root";

$conn=mysql_connect($local,$root);
mysql_select_db("user");
mysql_query("set names utf8");

$sql="insert into yinfu(name,password)
values('{$_POST['name']}','{$_POST['password']}')";
mysql_query($sql);

$_POST="submit";
if($_POST['submit']){
$sql="select * from yinfu where name={$_POST['user']}";
$row=mysql_query($sql);
if($row>0){
echo "用户名存在";
exit();
}else{

echo "可以注册";
}
}

?>

下面是我的html文件
<!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>
</head>

<body>
<form action="1.php" method="post">
用户名:<input type="text" name="user"><br />
密码:<input type="text" name="pass">
<input type="submit" value="提交" name="submit" />
</form>
</body>
</html>
最佳答案
  • 五星知识达人网友:笑迎怀羞
  • 2021-01-31 21:20
header("Content-Type:text/html;charset=utf8");
$local="localhost";
$root="root";

$conn=mysql_connect($local,$root);
mysql_select_db("user");
mysql_query("set names utf8");

//if代码段应该放在插入语句之前,因为代码是从上往下执行的。
if($_POST['submit']){ 
 $sql="select * from yinfu where name={$_POST['user']}";
 $row=mysql_query($sql);
 if($row>0){
echo "用户名存在";
exit();
 }else{

          echo "可以注册";
}
}

$sql="insert into yinfu(name,password)
                     values('{$_POST['name']}','{$_POST['password']}')";
mysql_query($sql);

$_POST="submit"; //这句不要了。
全部回答
  • 1楼网友:不如潦草
  • 2021-01-31 21:56
用户注册的时候,把接受过来的用户名,在数据库里查询,如果为真,就代表有,禁止注册,如果没有,则注册通过
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯