2016-02-11 96 views
0

所以我試圖創建一個表單,允許名稱像Name_Lastname註冊名稱輸入的人。但它不工作,使用2個輸入字段名IM檢查名稱是否有效

名稱: 名字:

,然後我用sprintf混合這兩種進Name_Lastname,而偉大的工程,但後來當我想檢查一個輸入的名字有無效的字符檢查功能不起作用,即使我的名字是正確的,它顯示它不是。

函數應該禁止,如果用戶有任何東西,但第一姓氏和名字資本註冊,並_,例如:Joan_Mackey =有效

joan_Mackey = invalid 
joan0_Mackey = invalid 
Joan92_Mackey = invalid 

,但其沒有工作,這裏是代碼

if(isset($_POST['register_button'])) 
    {  

     $name = clear($_POST['firstname']); 
     $lastname = clear($_POST['lastname']); 

     $createdname = sprintf("%s_%s", $name, $lastname); 

     $ime = mysqli_real_escape_string($con, $createdname); 

     $email = clear($_POST['email']); 
     $pass = clear($_POST['password']); 
     $cpass = clear($_POST['confpassword']); 


     // Proveravanje ako ima _ i tih sranja 
     $query = "SELECT * FROM Igraci WHERE Ime = '$ime'"; 
     $stmt = mysqli_prepare($con, $query) or die(mysqli_error($con)); 
     mysqli_stmt_bind_param($stmt, "s", $ime); 
     mysqli_stmt_execute($stmt); 
     $result = mysqli_stmt_get_result($stmt); 
     $row = mysqli_fetch_array($result); 

     if(preg_match("([A-Z]{1,1})[a-z]{2,9}+_([A-Z]{1,1})[a-z]{2,9}", $createdname)) 
     { 
      if($row) // Ime nije registrovano kod dole 
      { 
       echo "Uneseno ime: '$ime' "; 
      } 
      else // Ime je vec registrovano, kod dole 
      { 
       echo "Uneseno ime: '$ime' "; 
      } 
     } 
     else 
     { 
      echo "format imena nije validan."; 
      exit(); 
     } 
    } 
+1

你從貨物邪教編程的痛苦。爲什麼當你在查詢中有** NO **佔位符時,首先使用參數綁定?如果您使用帶有參數綁定的預處理語句,爲什麼要手動轉義這些值? –

+2

[Falsehoods Programmers Believe About Names](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) –

+0

另一種逃避價值的方式是什麼?我只知道真正的逃避 – Nathaniel

回答

0

簡單地改變

if(preg_match("([A-Z]{1,1})[a-z]{2,9}+_([A-Z]{1,1})[a-z]{2,9}", $createdname)) 

if(preg_match("^[A-Z][a-z]{2,9}_[A-Z][a-z]{2,9}$", $createdname)) 

這將允許通過使用3到9個字符的名字和具有適當大小寫的3到9個字符的姓氏來構建用戶名稱。

說明:

^ = Start at beginning of string 
[A-Z] = One capital letter 
[a-z]{2,9} = two to nine small letters 
_ = one underscore 
[A-Z] = One capital letter 
[a-z]{2,9} = two to nine small letters 
$ = Match must go up to end of string 
+0

是的,正是我想說的。 –

+0

不,不工作,我輸入正確的名稱,但仍然顯示它不正確 http://i.imgur.com/aG9u4U8.jpg – Nathaniel

+0

編輯:我想我現在得到它,請檢查,如果你可以爲我: (preg_match(「/ ^([AZ] {1,1})[az] {2,9} + _([AZ] {1,1})[az] {2,9} $ /「,$ createdname)) – Nathaniel