It's a little embarrassing to admit but I've had this problem for over a week. I'm trying to get data from a form into my database, codewise everything seems to be perfect, yet I'm getting only "Connection OK" reply from the connect.php file mentioned at the bottom. No response and no results in the database.
I've double checked the path to the file and added both 'name' and 'id' to the form.
This is my form:
<form action="registration.php" method="POST">
<div class="registration_wrapper">
<button class="registration_button">Registration ⯆</button>
<input type="text" class="user_input_fields" id="first_name" name="first_name" placeholder="First Name" required>
<input type="text" class="user_input_fields" id="last_name" name="last_name" placeholder="Last Name" required>
<input type="text" class="user_input_fields" id="email" name="email" placeholder="E-mail" required>
<input type="text" class="user_input_fields" id="dob" name="dob" placeholder="Date of Birth" required>
<input type="text" class="user_input_fields" id="username" name="username" placeholder="Username" required>
<input type="text" class="user_input_fields" id="password1" name="password1" placeholder="Password" required>
<input type="text" class="user_input_fields" id="password2" name="password2" placeholder="Repeat Password" required>
<input type="checkbox" name="agreements">I agree to the <a href="Terms_and_conditions.php"><u>Terms & Conditions</u></a>
<button type="submit" class="continue_button">Continue ></button>
</div>
</form>
This is the registration.php file:
<?php
$page_title = 'Registration';
if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
require ('connect_db.php');
$errors = array();
if ( empty( $_POST[ 'first_name' ] ) )
{ $errors[] = 'Enter your first name.' ; }
else
{ $fn = mysqli_real_escape_string( $link, trim( $_POST[ 'first_name' ] ) ) ; }
# Check for a last name.
if (empty( $_POST[ 'last_name' ] ) )
{ $errors[] = 'Enter your last name.' ; }
else
{ $ln = mysqli_real_escape_string( $link, trim( $_POST[ 'last_name' ] ) ) ; }
# Check for an email address:
if ( empty( $_POST[ 'email' ] ) )
{ $errors[] = 'Enter your email address.'; }
else
{ $e = mysqli_real_escape_string( $link, trim( $_POST[ 'email' ] ) ) ; }
if ( empty( $_POST[ 'username' ] ) )
{ $errors[] = 'Enter your email address.'; }
else
{ $un = mysqli_real_escape_string( $link, trim( $_POST[ 'username' ] ) ) ; }
# Check for a password and matching input passwords.
if ( !empty($_POST[ 'password1' ] ) )
{
if ( $_POST[ 'password1' ] != $_POST[ 'password2' ] )
{ $errors[] = 'Passwords do not match.' ; }
else
{ $p = mysqli_real_escape_string( $link, trim( $_POST[ 'password1' ] ) ) ; }
}
else { $errors[] = 'Enter your password.' ; }
# Check if email address already registered.
if ( empty( $errors ) )
{
$q = "SELECT user_id FROM users WHERE email='$e'" ;
$r = mysqli_query ( $link, $q ) ;
if ( mysqli_num_rows( $r ) != 0 ) $errors[] = 'Email address already registered. <a href="login.php">Login</a>' ;
}
# On success register user inserting into 'users' database table.
if ( empty( $errors ) )
{
$q = "INSERT INTO users (first_name, last_name, email, u_password, u_datejoined) VALUES ('$fn', '$ln', '$e','$un',SHA1('$p'), NOW() )";
$r = mysqli_query ( $link, $q ) ;
if ($r)
{ echo 'Registration successful'; }
# Close database connection.
mysqli_close($link);
exit();
}
# Or report errors.
else
{
echo '<div class="container"><h1>Error!</h1><p id="err_msg">The following error(s) occurred:<br>' ;
foreach ( $errors as $msg )
{ echo " - $msg<br>" ; }
echo 'Please try again.</p></div>';
# Close database connection.
mysqli_close( $link );
}
}
?>
This is the connect.php file:
<?php
# establishing link to database using username, password and database name
$link = mysqli_connect('localhost', 'root', 'root', 'tecbooks');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error());
}
echo '<h1>Connection OK</h1>';
mysqli_set_charset( $link, 'utf8' ) ;
?>
Look at the insert query this way:
$q = "INSERT INTO users (first_name, last_name, email, u_password, u_datejoined)
VALUES ('$fn', '$ln', '$e', '$un', SHA1('$p'), NOW() )";
What do you notice? Something's missing.
So $r
is false here: $r = mysqli_query ( $link, $q ) ;
. Database connection is closed, program exits.
It turned out to be a database structure problem unrelated to the code. The users in the database were assigned unique IDs. The problem was that the value was not automatically incremented and therefore the record could not be inserted. Here's a link to the solution: