我有以下功能,在单词中应该获取用户凭据并验证它们。如果它们有效,则将它们插入到数据库中。插入后检查它们是否已成功插入。如果没有返回false。如果电子邮件首先无效,则返回false。 Eclipse + Aptana插件报告了最后一个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
答案 0 :(得分:3)
if((filter_var($email, FILTER_VALIDATE_EMAIL))
&& preg_match('/@.+\./', $email)); { //<<<REMOVE THE ;
答案 1 :(得分: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和参数化查询。
答案 2 :(得分: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;
}
答案 3 :(得分:1)
你看到这个小分号吗? if((filter_var($email, FILTER_VALIDATE_EMAIL)) && preg_match('/@.+\./', $email));
答案 4 :(得分: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;
}
}
答案 5 :(得分:-2)
为什么最后有两个else {}
块...?你只能有一个。