2014-11-05 75 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

相關問題