0

I want to specify a raw username/password into an SQL query with PHP:

function doRegister($username, $password) {
    $db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME) or die('error');
    $query = "SELECT username FROM users WHERE username = $username";
    $result = $db->query($query);
    if (mysqli_num_rows($result) == 1) {
        $msg = 'Username already taken';
    } else {
        $register = "INSERT INTO users(username, password)" .
                    "VALUES($username, SHA($password))";

        //error happens here
        $db->query($register) or die('error registering your account');


        $msg = "Register successful";
    }
    $db->close();
    echo $msg;
}

I am getting an error at $db->query($register). What am I doing wrong?

3
  • paste the results of mysql command show users; also what mysql error do you get, specifically? Commented Nov 16, 2011 at 21:14
  • 4
    WARNING your code may be susceptible to sql injection! Commented Nov 16, 2011 at 21:14
  • @DanielA.White - MAY be? I'd consider that actually susceptible, even if the input parms are (currently) being escaped in the calling method - There's too much of a possibility of a maintainer assuming things are safe elsewhere. Something like this (sepecially like this) should of course use parameterized queries. Commented Nov 16, 2011 at 23:22

2 Answers 2

6

You have not surrounded your $username or $password in quotes, so when passing an integer it works correctly but won't accept a string:

$query = "SELECT username FROM users WHERE username = '$username'";
//----------------------------------------------------^^^^^^^^^^^^

$register = "INSERT INTO users(username, password)" .
                    "VALUES('$username', SHA('$password'))";
//-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Please be certain that you have properly escaped these values with mysql_real_escape_string().

These columns should really not be type TEXT. They ought to be VARCHAR(). Perhaps your VARCHAR() attempt failed because you were missing the length parameter, as in VARCHAR(32) for max 32 characters.

Sign up to request clarification or add additional context in comments.

3 Comments

Make sure to also quote $username in SELECT username FROM users WHERE username = $username.
@Rocket Good catch, added above.
Actually i didn't miss the length parameter but i thought the problem is with the VARCHAR itself so i moved to TEXT, but you were right about the quotes, problem solved, thanks
1

Try this:

       function doRegister($username, $password) {
        $db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME) or die('error');
        $query = "SELECT username FROM users WHERE username = '$username'";
        $result = $db->query($query);
        if (mysqli_num_rows($result) == 1) {
            $msg = 'Username already taken';
        } else {
            $register = "INSERT INTO users(username, password)" .
                    "VALUES('$username', SHA('$password'))";
            $db->query($register) or die('error registering your account');
            $msg = "Register successful";
        }
        $db->close();
        echo $msg;
    }

1 Comment

You should also quote $username in SELECT username FROM users WHERE username = $username.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.