我对编程有点新意,我正在为我的注册表单编写一份准备好的声明。部分验证是检查并查看电子邮件地址是否已存在。我遇到的问题是没有插入数据,当我检查电子邮件是否已经存在并且我被重定向到未定义的页面时,代码似乎停止工作。 www.mywebsite.com/undefined而不是profile.php。检查我的日志时,我没有收到任何错误。我不明白为什么会这样做,如果有人能帮助/教我我做错了什么,我将不胜感激。 Mysqlnd已启用且API扩展正在运行。我检查了我的phpinfo(),并且正在发送和接收数据。
signup.php
<?php
session_start();
include '../dbh.php';
$respond = array(
'status' => true,
'message' => 'There was an error',
'redirect',
'errors'
);
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$errorEmpty = false;
$errorEmail = false;
if (empty($first) || empty($last) || empty($email) || empty($pwd)) {
$respond['errors'][] = "Please fill out all fields!";
$respond['errorEmpty'] = true;
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$respond['errors'][] = "Please enter a valid email address!";
$respond['errorEmail'] = true;
} else {
$sql = "SELECT email FROM user WHERE email= ? ";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, 's', $email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
$respond['errors'][] = "That email address already exists!";
$respond['errorEmail'] = true;
}
else {
$encryptpwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "INSERT INTO user (first, last, email, pwd) VALUES (?,?,?,?)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, 'ssss', $first, $last, $email, $password_hash);
if (mysqli_stmt_execute($stmt)) {
$userID = mysqli_insert_id($conn);
$status = 1;
$sql2 = "INSERT INTO profileImg (email, status) VALUES(?,?)";
$stmt2 = mysqli_prepare($conn, $sql2);
mysqli_stmt_bind_param($stmt2, 'si', $email);
mysqli_stmt_execute($stmt);
$_SESSION['id'] = $userID;
$respond['redirect'] = "../profile.php?id=$userID"; // redirect to the actual ID of the user who just signed up
}
}
}
}
echo json_encode($respond);
?>
profile.php
<?php
session_start();
include 'dbh.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Yahbang</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header class="header_profile">
<nav>
<ul>
</ul>
</nav>
<form class="logout" action='include/logout.inc.php'>
<button>Log out</button>
</form>
</header>
<?php
$sql = "SELECT * FROM user";
$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 id='$id'";
$resultImg = mysqli_query($conn, $sqlImg);
while ($rowImg = mysqli_fetch_assoc($resultImg)) {
echo "<div class='userProfileImage'>";
if ($rowImg['status'] == 0 ) {
echo "<img src='images/profile".$id.".jpg'>";
} else {
echo "<img src='images/profile_default.jpg'>";
}
echo "<p>".$row['first']."</p>";
echo "</div>";
}
}
} else {
echo "There are no users yet!";
}
if (isset($_SESSION['id'])) {
echo "<form action='include/uploadProfile.inc.php' method='POST' enctype='multipart/form-data'>
<input type='file' name='file'>
<button type='submit' name='submit'>UPLOAD</button>
</form>";
} else {
header("Location: ../index.php");
exit();
}
?>
<footer class="footer_profile">
<nav>
<ul>
</ul>
</nav>
</footer>
</body>
</html>