1

hi guys please l need some help. lm setting up a user registration sign up form. but l was NOT ABLE TO INSERT THE USER INFO IN TO THE DATABASE. And the code did not display any error message, meaning that everything is fine. But when l tried to sign up it gives the form's error message "Failed to Register User".

this is the code: (and check at the bottom the connect.php code)

<?php

require_once('connect.php');
//print_r($_POST);
if(isset($_POST) & !empty($_POST)) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // storing the sign up info in to the database
    $sql = "INSERT INTO usermanagement (username, email, password) VALUES ('$username', '$email', '$password')";

    //executing the query
    $result = mysqli_query($connection, $sql);

    if($result){
        echo "User Registered Successfully";
    }
    else{
        echo "Failed to Register User";
    }
}

?> 


    <div id="form-signup">
                    <form id="signup-form" name="sign-up" action="sign.php" method="POST">
                            <h1>Create your profile</h1>
                            <p>
                                <input type="text" name="username" id="username" class="signup-input" required="required" placeholder="Full name*" >
                            </p>
                            
                            <p>
                                <input type="email" name="email" class="signup-input" required="required" placeholder="Email*" >
                            </p>
                            <p>
                                <input type="password" name="password" class="signup-input" required="required" placeholder="Mot de passe*">
                            </p>
                            <!-- <p>
                                <input type="password" name="confirmpassword" class="signup-input" required="required" placeholder="Confirmez mot de passe*">
                            </p> -->
                            <p class="agree"> By signing up, you agree to Tout-Passe's <br><a href="condition.php"> Terms of use</a><br> and <a href="politic.php">Privacy Policy</a>.
                            </p>
                            <p>
                                <input type="submit" class="signup-btn" name="btn-signup" value="Create Account">
                            </p>
                            <p class="already">Already on Tout-Passe? <a href="log.php"> Log in</a></p>
                        </div><!--END OF PHASE-1-->
                    </form> <!--END OF SIGN-UP-->
                </div><!--END FO ALLFORM-->

CONNECT CODE

<?php 
$connection = mysqli_connect('localhost', 'root', '');
if(!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'listing');//listing = database
if(!$select_db){
    die("Failed to select database" . mysqli_error($connection));
}

?>
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Bachir
  • 43
  • 3
  • thank you again. l did what you said and it did gave me this : Failed to Register User, reason: Field 'active' doesn't have a default value – Bachir Jun 03 '17 at 22:33
  • but l chose for the "active field: BOOLEAN", and when l saved it it became( tinyint(1)). – Bachir Jun 03 '17 at 22:35
  • And it was the exact same in the tutorial l watched – Bachir Jun 03 '17 at 22:36
  • 1
    Please, update your question, and provide the sql to create the table. – Vulkhan Jun 03 '17 at 22:43
  • try this sql request `INSERT INTO usermanagement (username, email, password, active) VALUES ('$username', '$email', '$password', 1)` – Vulkhan Jun 03 '17 at 22:51

1 Answers1

2

You did a mistake line 4, if(isset($_POST) & !empty($_POST)) { you have only one & you should have two like this: if(isset($_POST) && !empty($_POST)) {

Beside this, when your message tell you that it failed to register the user, it does not explain why, to solve this you have to add a mysqli_error() like this:

require_once('connect.php');
//print_r($_POST);
if(isset($_POST) && !empty($_POST)) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // storing the sign up info in to the database
    $sql = "INSERT INTO usermanagement (username, email, password) VALUES ('$username', '$email', '$password')";

    //executing the query
    $result = mysqli_query($connection, $sql);

    if($result){
        echo "User Registered Successfully";
    }
    else{
        echo "Failed to Register User, reason: " . mysqli_error($connection);
    }
}

Also there is two problems with your code:

  1. It is faillibe to SQL injection. What is SQL injection?
  2. You should test each of your global variables are set individually like this: if(isset($_POST['username']) && !empty($_POST['username'])) and not like this: if(isset($_POST) && !empty($_POST))
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Vulkhan
  • 438
  • 5
  • 15
  • ah ok thank you. l just corrected what you said, but l still cannot store the user data in to the database – Bachir Jun 03 '17 at 22:10