标头功能不起作用

时间:2018-05-19 08:46:40

标签: php if-statement mysqli phpmyadmin

我创建了一个注册表单,一旦用户成功注册,它应该重定向到home.php。但相反,它被重定向到index.php。其他一切正常(请参阅下面的代码)。我可以看到用户已添加到我的数据库中,并且配置文件图片的状态为1.

<?php

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

include_once 'dbh.inc.php';

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['passwordS'];

if (empty($first) || empty($last) || empty($email) || empty($username) || empty($password)) {
    header("Location: ../signup.php?signup=empty"); exit();}
else{
    if (!preg_match("/^[A-Za-z\s'-]{2,50}$/", $first) || !preg_match("/^[A-Za-z\s'-]{2,50}$/", $last)) {
        header("Location: ../signup.php?signup=flnameinvalid&email=$email&username=$username"); exit();}
    else{
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: ../signup.php?signup=emailinvalid&first=$first&last=$last&username=$username"); exit();}
        else{
            $mysql = "SELECT * FROM users WHERE user_username='$username';";
            $result = mysqli_query($conn, $mysql);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                header("Location: ../signup.php?signup=unametaken&first=$first&last=$last&email=$email"); exit();}
            else{
                if (strlen($password) < 8) {
                    header("Location: ../signup.php?signup=strlen&first=$first&last=$last&email=$email&username=$username"); exit();}
                else{
                    if (!preg_match("#[0-9]+#", $password)) {
                        header("Location: ../signup.php?signup=num&first=$first&last=$last&email=$email&username=$username"); exit();}
                    else{
                        if (!preg_match("#[A-Z]+#", $password)) {
                            header("Location: ../signup.php?signup=cap&first=$first&last=$last&email=$email&username=$username"); exit();}
                        else{
                            $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                            $sql = "INSERT INTO users (user_first, user_last, user_email, user_username, user_password) 
                                    VALUES (?, ?, ?, ?, ?);";
                            $stmt = mysqli_stmt_init($conn);

                            if (!mysqli_stmt_prepare ($stmt, $sql)) {
                                echo "SQL error!";}
                            else{
                                mysqli_stmt_bind_param ($stmt, "sssss", $first, $last, $email, $username, $hashedPwd);
                                mysqli_stmt_execute ($stmt);



                                $sql3 = "SELECT * FROM users WHERE user_username='$username' AND user_first='$first';";
                                $result = mysqli_query($conn, $sql3);

                                if (mysqli_num_rows($result) > 0) {
                                    while ($row = mysqli_fetch_assoc($result)) {
                                        $userid = $row['id'];
                                        $sql4 = "INSERT INTO profileimg (userid, status) VALUES ($userid, 1);";
                                        mysqli_query($conn, $sql4);
                                    }
                                }
                                else{ echo "There are no users!";}
                            }
                            header("Location: ../home.php?signup=success"); exit();
                        }
                    }
                }
            }
        }
    }
}
}

else{header("Location: ../signup.php?signup=error");}

即使我将它放在mysqli_execute($ stmt)之后,标题函数也不起作用。 我尝试创建一个用户定义的函数,但它仍然无法正常工作。 昨晚我还没有添加最后一个if和else语句,那个是带有$ sql3的语句。

=========更新==========

不知何故,我能够确定它所遵循的重定向是我home.php的最后一个else语句中的头函数。这是代码

    <div class="sideright">
        <?php
            $sql = "SELECT * FROM users";
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $id = $row['id'];
                    $sqlImg = "SELECT * FROM profileimg where userid='$id'";
                    $resultImg = mysqli_query($conn, $sqlImg);
                    while ($rowImg = mysqli_fetch_assoc($resultImg)) {
                        echo "<div>";
                        if ($rowImg['status'] == 0) {
                            echo "<img src='uploads/profile".$id.".$fileActualExt'".mt_rand()." height='200' width='200'>";}
                        else{
                            echo "<img src='uploads/defaultprofilepicture.jpg' height='200' width='200'>";}
                        echo $row['user_username'];
                        echo "</div>";
                    }
                }
            }
            else{
                echo "Sorry, an error occurred.";}


            if (isset($_SESSION['key'])) {
                echo 
                '<form action="upload.php" method="POST" enctype="multipart/form-data">
                <input type="file" name="file" value="Change Profile Picture">
                <button type="submit" name="submitFile"> Go </button>
                </form>';
                echo 
                '<form action="includes/logout.inc.php" method="POST">
                <button type="submit" name="submitLogout"> Log Out </button>
                </form>';
            }
            else{
            header("Location: index.php");}

我仍然无法修复它,请帮我找出错误的地方。

2 个答案:

答案 0 :(得分:1)

尝试

echo "<script type=\"text/javascript\">".
       "window.location='signup.php?signup=error';".
      "</script>";

答案 1 :(得分:0)

你可以试试这个,它为我工作。

将此代码替换为标题位置

<script type=\"text/javascript\">
    window.location.href='home.php';
</script>