2014-05-09 80 views
-1

這一整天都在工作,訪問了很多論壇,並接受了很多建議,但似乎仍然有些問題。無法弄清楚爲什麼我的查詢不會通過

我試圖通過SQL提交查詢,一旦所有正確的用戶信息被輸入,然後重定向用戶返回index.html作爲一個'登錄'用戶一旦完成。

下面是代碼:

error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 
//setup database connection 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$mysql_database = "21st"; 

$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Could not connect database"); 
     mysqli_select_db($conn, $mysql_database) or die("Could not select database"); 

//get user info  
$username = $_POST['username']; 
$email = $_POST['email']; 
$password = $_POST['password']; 
$password2 = $_POST['password2']; 
//if passwords are equal and longer than 5 characters, and username is longer than 4 characters 
if ($password != $password2) || (strlen($username)) < 4 || (strlen($password < 5)) { 
     header('Location: index.html'); 
} else { 
     //encrypt passwords 
     $password = md5($password); 
     $password2 = md5($password2); 
     //sanitize email 
     $sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL); 
     //insert query into database 
     $sql = "INSERT INTO members (username, email, password) VALUES ('$username','$email','$password')"; 
     mysqli_query($conn, $sql); 
     //redirect user to index.html once all is completed 
     header('Location: index.html'); 
     exit(); 
    } 
} 
+1

你得到任何錯誤? –

+2

md5不安全的密碼,[看到這個例子](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords) – kero

+0

你是否檢查過列中的列名數據庫是否與您在SQL查詢中指定的列名相同? –

回答

0

您的編碼錯誤:

if ($password != $password2) || (strlen($username)) < 4 || (strlen($password < 5)) { 
     header('Location: index.html'); 
} 

應該

(strlen($username) < 4) 
(strlen($password) < 5) 

編輯

if else語法不正確(docs

正確的代碼應該是這樣的:

if (($password != $password2) || (strlen($username) < 4) || (strlen($password) < 5)) { 
    header('Location: index.html'); 
} else { 
     // Handle your false 
} 
+0

同時爲'header('Location:index.html');'添加一個參數來查看哪個頭部正在發射.. ie - 'header('Locaiton:index.html?c = 1');'和'header( 'Locaiton:index.html?c = 2');' –

+0

是的,它永遠不會像以前那樣評價爲真,所以它不應該是這樣嗎? :) –

+0

但它不會計算爲false,因爲整個if語句需要'()'在整個條件下都有語法錯誤 –

相關問題