<?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>