无法弄清楚为什么我的查询不会通过

时间:2014-05-09 23:18:04

标签: php registration

现在一整天都在工作,访问了很多论坛,并且已经采取了很多建议,但似乎还有一些错误。

我尝试在输入所有正确的用户信息后将查询提交到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 个答案:

答案 0 :(得分: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
}