【发布时间】:2018-10-29 14:40:39
【问题描述】:
我创建了一个注册表单,一旦用户成功注册,它应该重定向到 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");}
我还没有解决,请帮我看看哪里出错了。
【问题讨论】:
-
如果删除最后一个 if 和 else 是否仍然有效?
-
(不是错误,而是...)值得检查 php.net/manual/en/mysqli-stmt.insert-id.php 以省去进行额外查询的麻烦。
-
您可以将它们存储在会话中并在没有任何参数的情况下重定向到页面,而不是通过重定向发送这么多值。然后在将这些会话放回表单/显示消息后取消设置。
-
@BhumiShah,不,它不再工作。好吧,我也在其他文件中添加了一些代码。我更新了我的帖子,你能看看吗?
-
你在 $_SESSION['key'] 中获得价值了吗?
标签: php if-statement mysqli phpmyadmin