如何检查已存在于数据库中的用户更改用户名

时间:2014-11-05 06:05:42

标签: php

     // php code start------------->

           <?php             

        // define variables and set to empty values
        $nameErr=$empidErr=$usernameErr=$passwordErr="";
        $name=$empid=$username=$password="";


        if(isset($_POST['submit']))
            {
        if (empty($_POST["empid"])) {
             $empid = "";
           } else {
             $empid = test_input($_POST["empid"]);
           }
        if (empty($_POST["name"])) {
             $name = "";
           } else {
             $name = test_input($_POST["name"]);
           }
        if (empty($_POST["etype"])) {
             $etype = "";
           } else {
             $etype = test_input($_POST["etype"]);
           }
        if (empty($_POST["username"])) {
             $usernameErr = "Username is required";
           } else {
             $username = test_input($_POST["username"]);
             // check if name only contains letters and whitespace
             if (!preg_match("/[0-9A-Za-z ^-_@. ]*$/",$username)) {                                   
               $usernameErr = "Only letters and white space allowed"; 
             }
           }
        if (empty($_POST["password"])) {
             $passwordErr = "Password is required";
           } else {
             $password = test_input($_POST["password"]);
             // check if name only contains letters and whitespace
             if (!preg_match("/[0-9A-Za-z ^-_@. ]*$/",$password)) {                                   
               $passwordErr = "Only letters and white space allowed"; 
             }
           }
        }

        //collect the data
        function test_input($data) {
           $data = trim($data);
           $data = stripslashes($data);
           $data = htmlspecialchars($data);
           return $data;
        }


        if((strlen($name)>0)&&(strlen($empid)>0)&&(strlen($etype)>0)&&(strlen($username)>0)&&(strlen($password)>0))
        {

        include "connection.php";


   //Here to check the username is aleady present in database or not



        $query = mysql_query("SELECT * FROM signin WHERE username='$username' ", $con);

            //$result = mysql_query($query) or die('Error: ' . mysqli_error($con));


        if (mysql_num_rows($query) <=0) 
        {
           echo "<script>alert('User already Exists Change the username');</script>";
        echo"<script>window.location.href = 'signin.php';</script>";
        }
        else
        {

//如果数据库中不存在,则在数据库中创建新用户。

            $sql="INSERT INTO signin (emp_name,emp_id,emp_type,username,password,create_datetime)
                                        VALUES ('$name','$empid','$etype','$username','$password',now())";

            if (!mysqli_query($con,$sql)) {
          die('Error: ' . mysqli_error($con));
        }
        echo "<script>alert('New User Added Successfully');</script>";
        echo"<script>window.location.href = 'login.php';</script>";

        }

        mysqli_close($con);

        }

        ?>

//php code end------------<



         //html code------------------>



<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <fieldset>
            <legend> <b><i> Information</i></b></legend><br>
            Employee ID:-<input type="text" name="empid" placeholder="Enter Employee ID" size="10" value="<?php echo $rum1?>" readonly> 
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            Employee Name:-<input type="text" name="name" placeholder="Surname      Middlename        Father Name" size="50" value="<?php echo $rum2;?>" readonly>&nbsp; 
            &nbsp;&nbsp;&nbsp;&nbsp;
            Employee Type:-<input type="text" name="etype" placeholder="Type" value="<?php echo $rum3;?>" readonly><br /><br /> 
            Username:-<input type="text" name="username" id="loginid" placeholder="Username" size="30" value="<?php echo $unm;?>">&nbsp; 
            <span class="error">* <?php echo $usernameErr;?></span>&nbsp;&nbsp;&nbsp;&nbsp;<br /><br /> 
            Password:-<input type="password" id="password" name="password"  size="30">&nbsp; 
            <span class="error">* <?php echo $passwordErr;?></span>&nbsp;&nbsp;&nbsp;&nbsp;<br />
             </fieldset>
            <br>
            <input name="submit" type="submit"  value="Submit">&nbsp;&nbsp;&nbsp;
            <input name="reset" type="submit"  value="Reset">&nbsp;&nbsp;&nbsp;
            <br ><br >
            </form>
            </fieldset>
            </body>
            </html>

         //html code end---------------------<





    In above php code is work but i want to check username.if the username present in the database then give the alert as the user is already present in the database change the username please. So please sir or madam suggest any code or changes in this php code and suggest any solution to check the user present in database or not.if user first time register then new user is added and if user multiple second time register then give alert is user already register please do your login.

1 个答案:

答案 0 :(得分:0)

知道当前mysql_num_rows是否应该返回1或多个特殊情况

所以改变这个

if (mysql_num_rows($query) <=0) 
        {
           echo "<script>alert('User already Exists Change the username');</script>";
        echo"<script>window.location.href = 'signin.php';</script>";
        }

到此

if (mysql_num_rows($query) >0) 
        {
           echo "<script>alert('User already Exists Change the username');</script>";
        echo"<script>window.location.href = 'signin.php';</script>";
        }

不要使用mysql功能,因为它们已被摧毁。了解mysqliPDO