2015-08-21 62 views
0

我想弄清楚爲什麼下面的$ 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;" 
)); 
+0

首先嚐試設置'$ SQL = '測試';'來檢查它是否是原來的' $ sql'生成錯誤 – zeflex

回答

0

看起來像作者在輸入字段中的type屬性的值(「密碼」)之前忘記了引號。我可能是唯一一個曾經在Packtpub.com上發佈過的用於「Ext JS數據驅動的應用程序設計」一書真實代碼的例子。這讓我感覺特別。

我創建的模式(PHP $分貝變量 - MySQL的模式類似於數據庫)被稱爲「BOOK1」,並改變了數據庫憑據根/根的代碼以匹配我在Windows上安裝了MySQL的安裝。然後,我必須將用戶添加到狀態爲1的「用戶」表中才能使事情順利進行。

也許在MySQL上爲我使用的版本(6.3)編寫腳本是不同的,但創建表時語法也保持失敗。我不得不忽略除CREATE語句之外的所有行。我也排除了以「ENGINE」開頭的代碼。從第1章

原代碼:

SET NAMES utf8; 
SET FOREIGN_KEY_CHECKS = 0; 

DROP TABLE IF EXISTS `users`; 
CREATE TABLE `users` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `status` tinyint(1) NOT NULL DEFAULT '1', 
    `email` varchar(255) NOT NULL, 
    `passwd` char(40) NOT NULL, 
    `lastname` varchar(20) NOT NULL, 
    `firstname` varchar(20) NOT NULL, 
    `modified` datetime DEFAULT NULL, 
    `created` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; 

SET FOREIGN_KEY_CHECKS = 1; 

新代碼:

CREATE TABLE `users` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `status` tinyint(1) NOT NULL DEFAULT '1', 
    `email` varchar(255) NOT NULL, 
    `passwd` char(40) NOT NULL, 
    `lastname` varchar(20) NOT NULL, 
    `firstname` varchar(20) NOT NULL, 
    `modified` datetime DEFAULT NULL, 
    `created` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
); 
+1

也許考慮從另一本書中學習。你不應該使用任何仍然使用'mysql_ *'函數的書,因爲這些書已經被使用了好幾年了。 –

相關問題