0

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 &#x2BC6;</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' ) ;
?>
Daniel Klas
  • 37
  • 1
  • 5
  • 1
    did you debug it? comment connection ok line and try, Did you check your log? – M.Hemant Apr 12 '19 at 09:51
  • Few poinetrs, add this line **require ('connect_db.php');** to top, below page_title. and change this **$q = "SELECT user_id FROM users WHERE email='$e'" ;** to **$q = "SELECT user_id FROM users WHERE email=".$_POST[ 'email' ] ;** also check your db credentials. change **if (!$link) { die('Could not connect to MySQL: ' . mysqli_error()); } else{echo '

    Connection OK

    '; }**
    – Vishwa Apr 12 '19 at 09:53
  • You are displaying the `echo '

    Connection OK

    ';` without any condition. So it displays always.
    – mageDev0688 Apr 12 '19 at 09:54
  • @Vishwa I have implemented the changes, its a lot neater but I'm still only getting the 'Connection OK' message – Daniel Klas Apr 12 '19 at 10:10
  • @M.Hemant It's written in Notepad++, I'm searching for a debugger now – Daniel Klas Apr 12 '19 at 10:10
  • @DanielKlas did you implemented this too? if (!$link) { die('Could not connect to MySQL: ' . mysqli_error()); } else{echo '

    Connection OK

    '; } also I think M.Hemant didn't mean you to get a debugger, but use standard php debugging practices
    – Vishwa Apr 12 '19 at 10:11
  • @Vishwa Yes, I made that change. I'll just rebuild this from the ground up probably and try to insert only one value into the database, instead of handling the entire form :/ – Daniel Klas Apr 12 '19 at 10:16
  • comment everything in your registration.php file and below all try to get form values first. then try to check if they are correct and not empty. if it's successful, then try to check db connection. remember to include connectiondb.php at the top, before any if conditions. try to insert fake values first, then try replacing them with the previously confirmed form inputs – Vishwa Apr 12 '19 at 10:19
  • @Vishwa That's a good suggestion, I will do that once I'll be back from work. It was just that the code already contains precisely what I need and I didn't want to give it up, holding myself back in result. Thanks for giving me another perspective :) – Daniel Klas Apr 12 '19 at 10:38
  • done'em all and suffered enough. learn from experience, good luck :) – Vishwa Apr 12 '19 at 10:39
  • Your Insert statement contains five fields, but six values... – Johannes Apr 12 '19 at 11:56

2 Answers2

0

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.

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15
  • Thanks for spotting that! However, after the correction, I still had the problem. What it seems to be is the "mysqli_real_escape_string", after rebuilding the file from the ground up (wrong syntax). PHPmyadmin is really fussy with that. The correct syntax was: "$first_name = mysqli_real_escape_string($link, $_POST['first_name']);" – Daniel Klas Apr 13 '19 at 11:14
0

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:

Auto increment in phpmyadmin

Daniel Klas
  • 37
  • 1
  • 5