2014-11-05 46 views
-2
 // 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 ^[email protected] ]*$/",$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 ^[email protected] ]*$/",$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. 
+0

[**请不要在新代码**中使用'mysql_ *'函数](http://bit.ly/phpmsql)。他们不再被维护[并且被正式弃用](http://j.mp/XqV7Lp)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – esqew 2014-11-05 06:07:18

回答

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

相关问题