How can I echo the logging system error to another index file

I am trying to execute registration system errors in the main index.php file, but I am doing something wrong. Can someone explain what I am doing wrong and how to do it correctly?

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;

?>

      

+3


source to share


2 answers


$ the error is undefined.

<?php echo $error; ?>

      

it should be



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

      

or change your index.php file 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>

      

+4


source


if there is an error, mysql query without values ​​will return an error too

or die(mysql_error());

      



prevents any code from being executed after it is executed, so it does not return a result. try to remove this part first.

0


source







All Articles