0

我有以下功能,它应该采用用户凭据并验证它们。如果它们有效,则将它们插入数据库。插入后检查它们是否成功插入。如果不返回false。如果电子邮件一开始就无效,则返回 false。Eclipse + Aptana 插件报告最后一个 else 部分的语法错误。当我尝试使代码合理化时,它似乎应该正常运行。我可以确认所有的 {brackets} 都在正确的层次顺序

这是代码:

   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
4

6 回答 6

3
if((filter_var($email, FILTER_VALIDATE_EMAIL)) 
      && preg_match('/@.+\./', $email)); { //<<<REMOVE THE ;
于 2012-05-23T15:12:38.253 回答
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 和参数化查询。

于 2012-05-23T15:17:45.960 回答
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;
}    
于 2012-05-23T15:13:24.377 回答
1

你看到这个结尾的小分号了吗?if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email));

于 2012-05-23T15:14:10.453 回答
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;
    }

}
于 2012-05-23T15:15:36.107 回答
-2

为什么else {}最后有两个街区...?你只能拥有一个。

于 2012-05-23T15:13:19.523 回答