-2
    <?php

$mysqli = mysqli_connect('localhost', 'test', 'test123', 'test'); 

if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
    exit();
}

global $mysqli;
$username   = $_POST['username']; 
$password   = $_POST['password'];
$firstname  = $_POST['firstname'];
$lastname   = $_POST['lastname'];
$email      = $_POST['email'];

$exists = 0;
$result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1"); 
if ($result->num_rows == 1) { 
    $exists = 1; 
    $result = $mysqli->query("SELECT password from users WHERE password = '{$password}' LIMIT 1"); 
    if ($result->num_rows == 1) $exists = 2; 
} else {
    $result = $mysqli->query("SELECT email from users WHERE email = '{$email}' LIMIT 1"); 
    if ($result->num_rows == 1) $exists = 3; 
}

if ($exists == 1) echo "<p>Username already exists!</p>"; 
else if ($exists == 2) echo "<p>Username and Email already exists!</p>"; 
else if ($exists == 3) echo "<p>Email already exists!</p>"; 
else {
    # insert data into mysql database
    $sql = "INSERT  INTO `users` (`id`, `username`, `password`, `firstname`, `lastname`, `email`) 
            VALUES (NULL, '{$username}', '{$password}', '{$firstname}', '{$lastname}', '{$email}')";

    if ($mysqli->query($sql)) {
        //echo "New Record has id ".$mysqli->insert_id; 
        echo '<meta http-equiv="refresh" content="0; url=test.html">'; 
    } else {
        echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>"; 
        exit();
    }
}
?>      

I am trying to create a register page, so when the user fills in the relevant details it register them to the database in MYSQL and stores details.

Therfore they can then go ahead to login, the error I am having is 'Undefined index' showing on all variables - username, password, firstname, lastname and email.

I have added all these variables to my backend db but still nothing..

HTML code:

<body>
    <div class="container">
      <h1 class="text-center">Booking</h1>
      <form class="form-signin" method="POST" action="register.php">
        <h2 class="form-signin-heading">Register</h2>
        <div id="all-form">
        <h3>User Details</h3>
        <input required type="text" class="form-control" placeholder="Name">
        <input required type="text" class="form-control" placeholder="Email Address">
        <h4>Login Details</h4>
        <input required type="text" class="form-control" placeholder="Username">
        <input required type="password" class="form-control" placeholder="Password">
        <input required type="password" class="form-control" placeholder="Confirm Password">
        <button class="btn btn-primary btn-sm" type="register">Register</button>
        <p><a href="signup2.html">Already registered? Sign in here </a></p>
      </form>
      </div>
    </div>
    <!-- /container -->
  </body> 

2 Answers2

0

Try this, add

     error_log(print_r($_POST,true));

at the top of your php code. Check your console logs to see what's coming in. When I did this I found some very strange behaviour in post data.

john elemans
  • 2,578
  • 2
  • 15
  • 26
0

You must need add a name attribute in your input tags and then you can access them with the $_POST[] array. You didn't add any name attributes in your input tags. See the code:

<body>
<div class="container">
  <h1 class="text-center">Booking</h1>
  <form class="form-signin" method="POST" action="register.php">
    <h2 class="form-signin-heading">Register</h2>
    <div id="all-form">
    <h3>User Details</h3>
    <input required type="text" class="form-control" placeholder="Name" name="name">
    <input required type="text" class="form-control" placeholder="Email Address" name="email">
    <h4>Login Details</h4>
    <input required type="text" class="form-control" placeholder="Username" name="username">
    <input required type="password" class="form-control" placeholder="Password" name="password">
    <input required type="password" class="form-control" placeholder="Confirm Password" name="confirm_password">
    <button class="btn btn-primary btn-sm" type="submit">Register</button>
    <p><a href="signup2.html">Already registered? Sign in here </a></p>
  </form>
  </div>
</div>
<!-- /container -->

notice that here I have added the name attribute with all the input tags. Now whenever you are going access this name with $_POST[] array you will not get any "undefined index" error.