Mikee Ashklanov
Mikee Ashklanov

Reputation: 31

How do i echo success when inputting data is successful

How do i echo Success Message when the data is successfully inserted in database.

My error code in html

<p><?php echo display_error(); ?></p>

Funtion of Display Error

function display_error() {
    global $errors;

    if (count($errors) > 0){
        echo '<div class="alert alert-danger alert-dismissible show fade">
                      <div class="alert-body">
                        <button class="close" data-dismiss="alert">
                          <span>&times;</span>
                        </button><b><center>';
            foreach ($errors as $error){
                echo $error .'</b></center></div></div>';
            }
    }
}   

Code if data we're successfully inserted

if (count($errors) == 0) {
        $password = md5($password_1);

        if (isset($_POST['user_type'])) {
            $user_type = e($_POST['user_type']);
            $query = "INSERT INTO users (username, user_type, password) 
                      VALUES('$username', '$user_type', '$password')";
            mysqli_query($db, $query);
            header('location: home.php'); // I just made this just to know if the data were successfully inserted to my database
        }else{
            $query = "INSERT INTO users (username, user_type, password) 
                      VALUES('$username', 'user', '$password')";
            mysqli_query($db, $query);

        }
    }

Upvotes: 0

Views: 782

Answers (3)

Farhan khan
Farhan khan

Reputation: 1

OR you can use this code .

if(mysqli_query($DBconnection,$query))
  {
    echo '
       <div class="container">
        <div class="page-header">
            <h3>Registration Complete</h3>
        </div>
       </div>';


}

Upvotes: 0

Farhan khan
Farhan khan

Reputation: 1

<?php
// this is simplest way i am suggesting you .

$row=mysqli_query($db_connection,$your_query);
if($row)
{
$info['msg']='sucess';
}
// now call this variable in any div where you would like to show sucess message ,
?>

Upvotes: 0

Pupil
Pupil

Reputation: 23948

mysqli_query() returns TRUE on success of INSERT

Return Values

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

So, definitely, if your INSERT works, if will return TRUE.

Please add an if condition:

if (mysqli_query($db, $query)) {
 // .. Add code for printing success message.
}

Upvotes: 1

Related Questions