每次刷新页面时,它都会将相同的用户插入数据库

时间:2015-08-30 05:45:00

标签: php mysql

这是PHP代码

<?php
if(isset($_POST['Murad'])){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$userName=$_POST['username'];
$password=$_POST['pwd1'];
$userName = stripslashes($userName);
$password = stripslashes($password);
$email=$_POST['email'];
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "123";
$mysql_databse = "websiteusers";
$prefix = "";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
$sql = "INSERT INTO websiteusers 
       (fullname,lastname,userName,email,pass) 
       VALUES ( '$firstname', '$lastname','$userName', '$email','$password')";
        mysqli_select_db($bd,'websiteusers');
$retval = mysqli_query($bd,$sql );
if(! $retval )
{
  die('Could not enter data: ');
  return false;
}
else {echo "Entered data successfully\n";
   }

   $usernamecheck=mysqli_query($bd,"SELECT `userName` FROM `websiteusers` 
                             WHERE userName='$userName'");
if(mysqli_num_rows($usernamecheck)>=1){
   echo $userName." is already taken";
 return false;
 }header("Location: Main.php");}

?>

用户注册然后当他在他的个人资料页面刷新后再次插入相同的用户名。并且用户名和电子邮件在我的dt中是唯一的,它不能插入它并给出错误

3 个答案:

答案 0 :(得分:0)

您现在正在做的是在数据库中插入用户,然后检查用户是否存在。你必须移动一些代码。

$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
 $usernamecheck=mysqli_query($bd,"SELECT `userName` FROM `websiteusers` 
                             WHERE userName='$userName'");
if(mysqli_num_rows($usernamecheck)>=1){
   echo $userName." is already taken";
 } else {
$sql = "INSERT INTO websiteusers 
       (fullname,lastname,userName,email,pass) 
       VALUES ( '$firstname', '$lastname','$userName', '$email','$password')";
        mysqli_select_db($bd,'websiteusers');
$retval = mysqli_query($bd,$sql );
if(! $retval )
{
  die('Could not enter data: ');
}
else {
    echo "Entered data successfully\n";
}
}
    }

这样您首先检查用户是否已存在。如果 - 你杀了脚本,后面的代码没有被执行。否则,您将在DB中插入用户

答案 1 :(得分:0)

您可以做的是在表单成功提交后

  

您可以重置表单

  

将用户重定向到同一页面

if(! $retval )
{
  die('Could not enter data: ');
  return false;
}
else {
    echo "Entered data successfully\n";
     header("Location:samepagename.php");

   }

重置表单

      this.form.reset(); 

在表单成功提交后调用此方法

答案 2 :(得分:0)

试试这个:

<?php
    if(isset($_POST['Murad'])) {
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $userName=$_POST['username'];
        $password=$_POST['pwd1'];
        $userName = stripslashes($userName);
        $password = stripslashes($password);
        $email=$_POST['email'];
        $mysql_hostname = "localhost";
        $mysql_user = "root";
        $mysql_password = "123";
        $mysql_databse = "websiteusers";
        $prefix = "";

        $link = new PDO('mysql:dbhost='.$mysql_hostname.';dbname='.$mysql_database,$mysql_user, $mysql_password);
        $unamecheck = ("SELECT userName FROM websiteusers WHERE userName = :uname");
        $unamecheck = $link->prepare($unamecheck);
        $unamecheck->execute(array(':uname'=>$userName));
        if($unamecheck->rowCount() > 0) {
            echo "Username taken";
            die();
        } else {
            $add = ("INSERT INTO websiteusers (fullname, lastname, userName, email, pass) VALUES (:fname, :lname, :uname, :pass)");
            $add = $link->prepare($add);
            $add->execute(array(':fname'=>$firstname, ':lname'=>$lastname, ':uname'=>$userName, ':pass'=>$password));
            if($add->rowCount() > 0) {
                echo "Registration successful";
                header("Location: Main.php");
            } else {
                echo "Registration failed";
            }
        }
    }
?>