2012-05-23 3 views
0

인 경우 단어에 사용자 자격 증명을 사용하고 유효성을 검사해야하는 다음과 같은 기능이 있습니다. 유효하다면 db에 삽입하십시오. 일단 삽입되면 성공적으로 삽입되었는지 확인하십시오. 그렇지 않으면 false를 반환합니다. 전자 메일이 처음부터 유효하지 않은 경우 false를 반환합니다. Eclipse + Aptana 플러그인이 마지막 else 섹션의 구문 오류를보고합니다. 코드를 합리화하려고 할 때 올바르게 작동해야하는 것 같습니다.Else 문 구문 오류가

public function storeUser($name, $email, $password) { 
    $uuid = uniqid('', true); 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email)); { //validate the email 
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"); 

    // check for successful store 
    if ($result) { 
     // get user details 
     $uid = mysql_insert_id(); // last inserted id 
     $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
     // return user details 
     return mysql_fetch_array($result); 
    } else { 
     return false; //unsuccessful store 
    } 
    else { //THIS IS LINE 45 
     //not a valid email 
     return false; 
    } 
    } 
} 

그것은 다음과 같은 오류 반환 :

05-23 11:07:10.758: E/JSON(1601): <b>Parse error</b>: syntax error, unexpected T_ELSE in <b>/home/content/40/8529140/html/webapps/projects/functions/DB_Functions.php</b> on line <b>45</b><br /> 
05-23 11:07:10.758: E/JSON Parser(1601): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 
+2

45 번 라인은 무엇입니까? – Neal

+0

코드에서 내 주석보기 – KDEx

+2

코드를 제대로 들여 쓰기해야합니다. 누락되었거나 쓸모없는 대괄호를 찾는 데 도움이됩니다. – acme

답변

2

귀하의 else 문을 닫는 중괄호에 대한 잘못된 위치에보십시오. 두 번째 앞에 } 중 하나를 이동하면 else이 작동합니다. 실제로는 그렇지 않을 것입니다. 여러분은 또한 빗나간 ;을 가지고 있지만 다른 조언은 여전히 ​​있습니다.

코드를 더 일관성있게 들여 쓰면 이런 종류의 문제에 빠지지 않습니다. 또한

public function storeUser($name, $email, $password) { 
    $uuid = uniqid('', true); 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    if (//validate the email 
     filter_var($email, FILTER_VALIDATE_EMAIL) && 
     preg_match('/@.+\./', $email) 
     ) { 
     $result = mysql_query (
      "INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())" 
     ); 
     if ($result) { // check for successful store 
      // get user details 
      $uid = mysql_insert_id(); // last inserted id 
      $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
      // return user details 
      return mysql_fetch_array($result); 
     } else { 
      return false; //unsuccessful store 
     } 
    } else { 
     //not a valid email 
     return false; 
    } 
} 

는 다음 mysql_* 기능을 사용하지 마십시오. PDO 및 매개 변수화 된 쿼리 사용법을 익히십시오.

+1

** **는 강한 단어입니다 : - P – Neal

+0

들여 쓰기 형식에 관한 한 좋은 조언으로서, 좀더 불변하면 아마 그것을 잡는 데 도움이되었을 것입니다. – KDEx

3
if((filter_var($email, FILTER_VALIDATE_EMAIL)) 
     && preg_match('/@.+\./', $email)); { //<<<REMOVE THE ; 
+0

@KDEx 세미콜론을 제거하십시오! – Neal

-2

왜 할을 내가 올바른 계층 순서 여기

에있는 {괄호}을 모두 확인할 수있는 것은 코드입니다 마지막에 두 개의 else {} 블록이 있어야합니다 ...? 하나만 가질 수 있습니다.

+2

2 개의 if 문이 있습니다. – Neal

1

당신은 제대로 중괄호를 닫는되지 않습니다 그리고 당신은 외부 if 후 추가 세미콜론했다 :

if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email)) { //validate the email 
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"); 

    // check for successful store 
    if ($result) { 
     // get user details 
     $uid = mysql_insert_id(); // last inserted id 
     $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
     // return user details 
     return mysql_fetch_array($result); 
    } else { 
    return false; //unsuccessful store 
    } 
} // close of outer if 
else { 
    //not a valid email 
    return false; 
}  
+0

왜 downvote? – Tudor

+0

잘못된 오류 .... 그 첫 번째 if 문 뒤에 세미콜론이 있습니다 ... – Neal

+0

@Neal : 문제가 해결되었지만 바깥 쪽이 제대로 닫히지 않은 경우에도 수정되었습니다. – Tudor

1

는이 말에 작은 세미콜론을 보는가를? if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email));

0

브래킷을 잘못 배치했습니다. 이

public function storeUser($name, $email, $password) { 
    $uuid = uniqid('', true); 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email)) { //validate the email 
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"); 

    // check for successful store 
    if ($result) { 
     // get user details 
     $uid = mysql_insert_id(); // last inserted id 
     $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
     // return user details 
     return mysql_fetch_array($result); 
    } else { 
     return false; //unsuccessful store 
    } 
    } 
    else { 
     //not a valid email 
     return false; 
    } 

} 
+0

잘못된 오류 .... 그 첫 번째 if 문 다음에 세미콜론이 있습니다. – Neal

+0

@Neal Didnt that. Fixed – dInGd0nG

+0

당신은 아마 당신이 고쳐 줬던 것을 OP에 말할 것입니다 .. – Neal