这是我的代码
if ($row['username'] > 0) {
array_push($errors, "Username already exist");
die();
}
$passwordHash = password_hash($pass, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (username, password, user_type) VALUES (:username, :password, :user_type)";
$stmt = $connect->prepare($sql);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':password', $passwordHash);
$stmt->bindValue(':user_type', 'user');
$result = $stmt->execute();
if ($result) {
array_push($success, "Account Successfully Created!");
header('Location: /');
exit;
}
我如何将array_push($errors...
回显到同一页面,因为在发送表单后,它向我显示了白页但具有相同的URL。另外,在array_push($success...
中,我希望它回显另一页中的消息。
答案 0 :(得分:1)
对我来说不清楚的问题是, 但是,如果您要表达的是要传递给另一页的信息!
所以您可以像这样使用SESSION。
FirstPage.php
<?php
// Start the session before using it.
session_start();
if($row['username'] > 0) {
array_push($errors, "Username already exist");
// Create new session variable,
// Or reset the value of it if it's already exists.
$_SESSION['error'] = "Username already exist";
// Go to the "SecondPage.php"
header('Location: SecondPage.php');
die();
}
?>
SecondPage.php
<?php
// Start the session before using it.
session_start();
// Checking if there is a Session called "error".
if(isset($_SESSION['error']) && $_SESSION['error'] != "") {
// So, there is an session with "error" variable saved on the Client's website.
// Echo the saved "error" variable from the session.
echo $_SESSION['error'];
// Reset / Remove the error so it won't be visible next time we visit this page.
$_SESSION['error'] = "";
}
?>
答案 1 :(得分:0)
由于array_push()会将元素推到数组的末尾,因此可以使用end()函数:
echo end($errors);
在要访问的end函数内部使用数组的名称。 示例:https://paiza.io/projects/zIhh0wUf17hEdY1juVK26A