php - 我如何将注册系统的错误回显到另一个索引文件

标签 php echo registration

我试图将注册系统错误回显到主 index.php 文件,但我做错了什么。有人可以解释我做错了什么以及如何做对吗?

index.php 文件。

<?php 
session_start();
include "config.php"; 

?>

<div class="container">
<h1>Registration</h1>

<?php if(!empty($error)): ?>
<div class="alert alert-danger alert-dismissible">
<ul>
    <li>
        <?php echo $error; ?>
    </li>
</ul>
</div>
<?php endif; ?>

    <button class="close" data-dismiss="alert" aria-label="close">
        <span aria-hidden="true">&times;</span>
    </button>

    <form action="main.php" method="post">
        <p>Username: </p>
        <p><input type="text" name="username" autocomplete="off"></p>
        <p>Password: </p>
        <p><input type="password" name="password" autocomplete="off"></p>
        <p><button name="send" class="btn btn-primary" id="btn">Send</button></p>
    </form>

</div>
</div>

main.php 文件..

    <?php 
include "config.php";

if(isset($_POST['send'])) {
    $username = $_POST['username'];
    $password = $_POST(md5['password']);

    if(empty($_POST['username'])) {
        $error = "Username is empty<br/>";
    }

    if(empty($_POST['password'])) {
        $error = "Password is empty";
    }

    $query = mysqli_query($connect,"INSERT INTO register(username,password) VALUES('$username', '$password')")
    or die(mysql_error());
    echo "Registred";
}

$_SESSION['error'] = $error;

?>

最佳答案

$error 未定义。

<?php echo $error; ?>

应该是

<?php echo $_SESSION['error'];?>

或者如下修改你的index.php文件

<?php 
session_start();
include "config.php"; 
// added this line to your code
$error = (isset($_SESSION['error'])) ? $_SESSION['error'] : "";
?>

<div class="container">
<h1>Registration</h1>

<?php if(!empty($error)): ?>
<div class="alert alert-danger alert-dismissible">
<ul>
    <li>
        <?php echo $error; ?>
    </li>
</ul>
</div>
<?php endif; ?>

    <button class="close" data-dismiss="alert" aria-label="close">
        <span aria-hidden="true">&times;</span>
    </button>

    <form action="main.php" method="post">
        <p>Username: </p>
        <p><input type="text" name="username" autocomplete="off"></p>
        <p>Password: </p>
        <p><input type="password" name="password" autocomplete="off"></p>
        <p><button name="send" class="btn btn-primary" id="btn">Send</button></p>
    </form>

</div>
</div>

关于php - 我如何将注册系统的错误回显到另一个索引文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097620/

相关文章:

echo - 自定义每个 session 或用户的 amazon alexa 技能意图的槽值

php - 使用DOMDocument在现有HTML表中创建新列

php - Validation SSL 是否与 soap 直接相关?

php - onclick 禁用提交按钮

javascript - 为什么我的 ajax 调用返回 null

php - 在 mysql 中搜索并回显多个结果

licensing - 发布要通过注册购买的软件的步骤

visual-studio-2008 - 如何在 Visual Studio 2008 安装项目中使用 regasm 注册 .NET CCW

javascript - Node.js 注册表

php - 如何使用 PHP 7 和 MySQL 将文件从 POST 插入到 longblob 列中?