2

I am trying to complete a registration form for users to register to the site. I was using mysql_* earlier on but got advised that mysqli_* or pdo would be better due to mysql_* being depreciated in PHP7. So, here's my code. Every time I execute this code, I just get a blank page, my 'error' message doesn't appear nor does the 'New record created successfully!' message. I have looked about on the web and cannot find out the problem with this script.

<?php

if (isset($_POST['submit'])){

$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";

$username=$_POST['username'];
$firstname=$_POST['firstname'];
$middlename=$_POST['middlename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$recovery_email=$_POST['recovery_email'];
$password=$_POST['password'];

        if (!$_POST['username'] || !$_POST['firstname'] || !$_POST['surname'] || !$_POST['email'] || !$_POST['password']) {

            header('Location: http://makeupstudiofix.co.uk/user/register/?error=fields');
            exit();

        } else {

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO user_logins (username, firstname, middlename, surname, email, recovery-email, password)
            VALUES ('$username', '$firstname', '$middlename', '$surname', '$email', '$recovery_email', '$password')";

            $conn->exec($sql);
            echo "<Script>alert('New record created successfully')</script>";
        }
            catch(PDOException $e)
        {
            echo $sql . "<br>" . $e->getMessage();
        }
            echo 'error';
}
}

$conn = null;

?>

Any help is greatly appreciated.

Josh Murray
  • 619
  • 5
  • 18
  • If you're getting a blank screen, there's probably a syntax error in the script. Check the PHP or Apache error log on the server. – Barmar Dec 11 '15 at 01:12
  • @Barmar - I was having a look for an error log, but there was none when I was receiving this blank page. ANy other error I have had previously gave me an error log. – Josh Murray Dec 11 '15 at 15:34
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 11 '15 at 16:36
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Dec 11 '15 at 16:36

2 Answers2

2

Do go over my answer in its entirety. There are quite a few things that stand to go haywire here.


You're not getting errors because you're not checking for them on the PHP side.

Firstly, you see your recovery-email column?

It contains a hyphen and MySQL is interpreting that as recovery MINUS email, in thinking you want to do math.

Either wrap it in ticks:

(username, firstname, middlename, surname, email, `recovery-email`, password)

or rename it using an underscore recovery_email (unless that was a typo).

You're not seeing the syntax error for it, likely due to the first conditional statement and is never making it in there to start with.

Use error checking during testing:

Also consult:

Plus, make sure your form and its elements are not failing you. Your form requires to use a POST method and that all your inputs bear the name attributes for them.

I.e.: <input type="text" name="username"> etc.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Possible conflict:

However, there may be a conflict in variables here $username, $password where you are using the same variables for your POST arrays and your login credentials.

Another thing is that your code is dependant on this conditional statement:

if (isset($_POST['submit'])){...}

So, that may also be why you're getting a blank screen, and/or you have syntax errors. (Use error reporting for it).

Add an else{ echo "Submit button not set..."; } for it just above $conn = null;.

If it falls into that, then you'll know what to go after; the input probably does not have the submit name attribute for it.

I.e.:

<input type="submit" name="submit" value="SUBMIT">

or that your submit button may be a <button> without the type="submit" type.

That is unknown, since you did not post your HTML form in your question.


Footnotes:

Even though you're using PDO, your code is still open to an SQL injection.

Use a prepared statement


Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Other links:


Final notes:

As Logan pointed out in his answer, using a conditional !empty() is a better method to check for empty fields, however it wouldn't cause the blank screen you're getting.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thank you for your answer, although it has gotten rid of the horrible white screen, I do now get errors about variables not being created, but I'll fix that up. Thanks for the help and information on passwords, column names, etc. – Josh Murray Dec 11 '15 at 15:57
  • You can't do math in the column list of an `INSERT` statement. The hyphen should be causing a MySQL syntax error. And since he has `ERRMODE_EXCEPTION` set, that should signal an error. – Barmar Dec 11 '15 at 16:31
  • @Barmar True and as I stated in my answer; they probably haven't seen that error yet, because it most likely didn't make it in there yet, and probably because of their conditional statement for the submit input not being named. Least, that's what I took from it. – Funk Forty Niner Dec 11 '15 at 16:33
  • It could also be because of the `$username/$password` variable reuse. That would cause an error during `new PDO`, before he set the error handling mode. – Barmar Dec 11 '15 at 16:56
  • @Barmar Yes, that also; which when upon going over their code quite a few times, made additional edits to that effect and added it in my answer. I'm sure it was a mix of many things. – Funk Forty Niner Dec 11 '15 at 16:57
0

You should use something like bindParam() instead of putting the variables straight to your query.

$sql->bindParam(':username', $_POST["username"], PDO::PARAM_STR, 12);

Do it for the other variables to be binded in your query.

And instead of

(!$_POST['username'] || !$_POST['firstname'] || !$_POST['surname'] || !$_POST['email'] || !$_POST['password'])

it should be

(empty($_POST['username']) || empty($_POST['firstname']) || empty($_POST['surname']) || empty($_POST['email']) || empty($_POST['password']))/* DO IT FOR THE REST OF THE VARIABLES */

You should also think of hashing your password. You can try the simple but reliable password_hash().

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • 1
    You should also have to change the `VALUES()` first of your `INSERT` query with `...VALUES (:username, :firstname, :middlename, :surname, :email, :recovery_email, :password)` – Logan Wayne Dec 11 '15 at 00:36
  • Yes yes. Also, I deleted my comment before because I re-read the answer you had gave me. Cheers. – Josh Murray Dec 11 '15 at 00:38
  • I have implemented the above things you have suggested. Unfortunately, I am still receiving a white screen, do you know why? – Josh Murray Dec 11 '15 at 00:49
  • @JoshMurray - I have reversed your `if()` condition. Try my updated answer now. – Logan Wayne Dec 11 '15 at 00:51
  • I have made the correct changes to the if statement, uploaded it and still no change. Any chance you could 'register' and see it for yourself? Cheers – Josh Murray Dec 11 '15 at 00:54
  • @JoshMurray - You can try to debug your code. Echo anything inside your `else{}` if it does something (make sure all fields are filled-up). Or try removing temporarily the `try{}` and `catch{}` inside your `else{}` condition. And in your form, make sure that your submit button has the tags of `name="submit"`. – Logan Wayne Dec 11 '15 at 01:00
  • Nothing is working, I'll read more about what I'm trying to do online and re-code everything. Just to save confusion. Thank for the help. – Josh Murray Dec 11 '15 at 01:16