2013-05-08 70 views
0
session_start(); 
include 'assets/config.php'; 

if(isset($_POST['username'])){ 

    $queryIsUsername = ("SELECT count(user) FROM users WHERE user = '$_POST['username']'"); //Error 
    $actionQueryIsUsername = mysql_query($queryIsUsername); 
    while($rowIsUsername = mysql_fetch_array($actionQueryIsUsername)) { 
     $isUsername[] = $rowIsUsername['COUNT(user)']; 
    } 

    if($isUsername[0]="0"){ 

     header("Location: login.php?error=e1"); 
    } 

    else{ 
//do stuff 
} 

我不確定最新錯誤,這是我的錯誤。我刪除了if語句,錯誤消失了。PHP SQL語法錯誤?

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Applications/XAMPP/xamppfiles/htdocs/craftlist/index.php on line 7 
+0

爲什麼你圍繞在括號中的查詢字符串? – 2013-05-08 23:48:31

+0

這是我拾起的一個小習慣。 – OPatel 2013-05-08 23:49:54

回答

3

將其更改爲

$queryIsUsername = ("SELECT count(user) FROM users WHERE user = '".$_POST['username']."'"); 

但這不是SQL INJECTION安全!

1

插值數組變量時,不應在引號的周圍加上引號。

個人而言,我寧願串聯:

"....".mysql_real_escape_string($_POST['username'])."..."; 

這是最容易閱讀的可能語法。

0
$queryIsUsername = ("SELECT count(user) FROM users WHERE user = '$_POST['username']'"); //Error 

應該

$queryIsUsername = ("SELECT count(user) FROM users WHERE user = '".$_POST['username']."'"); //Error 

上的擱置你可能想看看庫MySQLi或PDO_MYSQL因爲PHP MySQL擴展已被棄用。見http://www.php.net/manual/en/intro.mysql.php