我想弄清楚爲什麼下面的$ sql echo語句在JavaScript中引發此錯誤。它來自$ sql賦值語句嗎?爲什麼會這樣?我只是通過一些用PHP編寫的書籍例子。ExtJS/PHP/MySQL:未捕獲SyntaxError:意外的令牌非法
Uncaught SyntaxError: Unexpected token ILLEGAL
<?php
session_start();
if (isset($_POST["exec"]) && $_POST["exec"] == "1") {
$url = "127.0.0.1";
$user = "root";
$pass = "root";
$db = "book1";
$message = "1";
echo "<script type='text/javascript'>alert('$message');</script>";
$link = mysql_connect($url, $user, $pass) or die("cannot connect.");
$sdb = mysql_select_db($db, $link) or die("faild select db");
$message = "2";
echo "<script type='text/javascript'>alert('$message');</script>";
$sql = implode("\n", array(
"SELECT",
" COUNT(id) as auth,",
" book1.users.email,",
" book1.users.firstname,",
" book1.users.lastname",
"FROM",
" book1.users",
"WHERE",
" book1.users.email = 'k'",
"AND",
" book1.users.passwd = SHA1(MD5('k'))",
"AND",
" book1.users.status = 1;"
));
echo "<script type='text/javascript'>alert('$sql');</script>";
$result = mysql_query($sql, $link) or die("faild query<hr/>SQL:" . $sql);
$row = mysql_fetch_assoc($result);
$auth = $row["auth"];
mysql_free_result($result);
mysql_close($link) or die("faild disconnect");
if ($auth == "1") {
$_SESSION["USERINFO"] = array(
"email" => $row["email"],
"firstname" => $row["firstname"],
"lastname" => $row["lastname"]
);
header("Location: ./index.php");
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
E-Mail:<input type="text" name="email" value=""/><br/>
Password:<input type=password" name="passwd" value="" /><br/>
<input type="submit" value="Login"/>
<input type="hidden" name="exec" value="1"/>
</form>
</body>
</html>
編輯:
這是用什麼代碼看起來像,但拋出了同樣的錯誤在瀏覽器控制檯窗口。
$sql = implode("\n", array(
"SELECT",
" COUNT(id) as auth,",
" users.email,",
" users.firstname,",
" users.lastname",
"FROM",
" users",
"WHERE",
" users.email = '" . mysql_real_escape_string($_POST['email']) . "'",
"AND",
" users.passwd = SHA1(MD5('" . mysql_real_escape_string($_POST['passwd']) . "'))",
"AND",
" users.status = 1;"
));
首先嚐試設置'$ SQL = '測試';'來檢查它是否是原來的' $ sql'生成錯誤 – zeflex