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 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并