dtwncxs3547 2015-06-28 08:31
浏览 36
已采纳

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

I'm trying to echo registration system errors to the main index.php file, but I'm doing something wrong. Can someone please explain what I've been doing wrong and how to do it right?

index.php file.

<?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 file..

    <?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;

?>
  • 写回答

2条回答 默认 最新

  • dpbz14739 2015-06-28 08:33
    关注

    $error is not defined.

    <?php echo $error; ?>
    

    should be

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

    or modify your index.php file as below

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus