I am working on an app where a user registers and its passed to the server where there information is added to the database.

It passes various checks along the way but ails to add the user.

Code below:

<?php

    $con = mysqli_connect('localhost', 'root', 'root', 'eat4lessaccess');

    //check connection
    if(mysqli_connect_errno())
    {
        echo "1: Connection Failed"; //Error code 1 = connection failed
        exit();
    }

    $username = $_POST["username"];
    $password = $_POST["password"];
    $name = $_POST["name"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];

    //Check username exists
    $usernamecheckquery = "SELECT username FROM useraccounts WHERE username='" . $username . "';";
    $usernamecheck = mysqli_query($con, $usernamecheckquery) or die("2: Username Check Query Failed"); //Error code 2 = username check query failed;

    if(mysqli_num_rows($usernamecheck) > 0)
    {
        echo "4: Username Already Exists";
        exit();
    }

    //Check email exists
    $emailcheckquery = "SELECT email FROM useraccounts WHERE email='" . $email . "';";
    $emailcheck = mysqli_query($con, $emailcheckquery) or die("3: Email Check Query Failed"); //Error code 2 = email check query failed;

    if(mysqli_num_rows($emailcheck) > 0)
    {
        echo "5: Email Already Exists";
        exit();
    }

    //Add User Account To Table
    $salt = "\$5\$rounds=5000\$" . "dajmsoftware" . $username . "\$";
    $hash = crypt($password, $salt);
    $insertuserquery = "INSERT INTO 'useraccounts' ('id', 'username', 'hash', 'salt', 'name', 'email', 'phone', 'stickers') VALUES (NULL, '" . $username . "', '" . $hash . "', '" . $salt . "', '" . $name . "', '" . $email . "', '" . $phone . "', '0')";
    echo $insertuserquery;
    mysqli_query($con, $insertuserquery) or die("6: Insert User Query Failed");

    echo "0";
?>

The console message i get creating a user is as follows:

Error Creating User Account. Error #INSERT INTO 'useraccounts' ('id', 'username', 'hash', 'salt', 'name', 'email', 'phone', 'stickers') VALUES (NULL, 'teajunkie', '$5$rounds=5000$dajmsoftwareteaj$xXqvpiBkwn77es6CWeVvlq/uzLJ/Eunak.exeYaRbS.', '$5$rounds=5000$dajmsoftwareteajunkie$', 'Daryl Ashby', '[censored Email Account]', '[Censored Phone Number]', '0')6: Insert User Query Failed

I MANUALLY CENSORED MY EMAIL & PHONE FROM THE CONCOLE LOG! :)

The id is set to primary and auto incriment.

Can someone help?

Many Thanks.

Recommended Answers

All 3 Replies

Instead of

mysqli_query($con, $insertuserquery) or die("6: Insert User Query Failed");

do this:

mysqli_query($con, $insertuserquery) or die("6: Insert User Query Failed" . mysqli_error($con));

and it might tell you why it fails in more detail.

Hello, thank you for replying.

Error Below:

Error Creating User Account. Error #6: Insert User Query FailedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''useraccounts' ('id', 'username', 'hash', 'salt', 'name', 'email', 'phone', 'sti' at line 1

Hello,

I figured out my issue. My syntax error was using ' instead of `

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.