2017-08-10 121 views
1

這是我的HAUNTED代碼。更改變量

ini_set("display_errors", true); 
ini_set("html_errors", false); 
require "conn.php"; 
echo "debug 1"; 
$stmt2 = $conn->prepare("SELECT * FROM UserData WHERE username = ?"); 
$stmt2->bind_param('s',$username); 
//$username = $_POST["username"]; 
$username ="netsgets"; 
$stmt2->execute(); 
$stmt2->store_result(); 
if ($stmt2->num_rows == 0){ // username not taken 
echo "debug 2.5"; 

die; 
}else{ 
// prepare query 
$stmt=$conn->prepare("SELECT * FROM UserData WHERE username = ?"); 
// You only need to call bind_param once 
$stmt->bind_param('s',$username); 
$username = "netsgets"; 
// execute query 
$stmt->execute(); 
$stmt->store_result(); 
// bind variables to result 
$stmt->bind_result($id,$dbUser,$dbPassword,$Type1,$Type2,$Type3,$Type4,$Type5); 
//fetch the first result row, this pumps the result values in the bound variables 

if($stmt->fetch()){ 
    echo 'result is ' . $Type1, $Type2,$Type3,$Type4,$Type5; 
} 


//var_dump($query2); 
echo "hi"; 

echo "debug 2"; 



echo "debug 2.7"; 

    if ($Type1 == "empty"){ 

     echo "debug 3"; 
     $sql11 = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?"); 
     $sql11->bind_param('ss',$TUsername,$Username); 
//  $TUsername = $_POST["TUsername"]; 
//  $Username = $_POST["username"]; 
     $TUsername = "test"; 
     $Username = "netsgets"; 
     $sql11->execute(); 



    } 
} 

這就是它返回的結果(回聲)。

Connected successfullydebug 1result is empty empty empty empty empty hidebug 2debug 2.7 

正如您所見,變量Type1,Type2,Type3,Type4,Type5都等於「空」。 由於某些原因,正如你所看到的,if語句不認爲它是「空的」,因爲它沒有回顯「debug 3」。什麼.....(也沒有錯誤。)

+0

您還沒有在任何地方聲明$ Type1等。 – Difster

+1

$ stmt-> bind_result($ id,$ dbUser,$ dbPassword,$ Type1,$ Type2,$ Type3,$ Type4,$ Type5); –

+0

難道不是嗎? –

回答

1

如果此代碼...

echo 'result is ' . $Type1, $Type2,$Type3,$Type4,$Type5; 

字面上產生這樣的輸出...

結果爲空空空空空

與之間的每一個「空」的空間,那麼顯然在數據庫中Type*值都是"empty "" empty"或索姆與前導/尾隨空白相結合的其他組合。

你會想要做的比較

trim($Type1) == 'empty' 

此外,如在評論中提到,在組合從未使用SELECT *bind_result。您應該始終明確所選擇的列以及它們出現的順序,例如

SELECT id, dbUser, dbPassword, Type1, Type2, Type3, Type4, Type5 FROM ... 
+0

謝謝菲爾。 –

+0

順便說一句,它仍然適用於'SELECT *' –

+0

當然,直到您添加或刪除列或您的數據庫決定更改訂單 – Phil