Is this code protected against SQL injection attacks? I'm just wondering if this code is good against SQL injection attacks. I don't know how to check if is good. Also I would like to know if is good how I'm working or this is just bad practice?

&lt;?php 

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

$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$salt = "Not important";
$password_hash = crypt($password, "$2y$". $salt);

if (strlen($username) &gt; 20) {
    echo "Username is too big";
}elseif (strlen($password) &gt; 32) {
    echo "Password is too big";
}elseif (strlen($email) &gt; 100) {
    echo "E-Mail is too big";
}elseif(strlen($username) == 0 || strlen($password) == 0 || strlen($email) == 0){
    echo "Fill every field";
}
else{
    $sth_username = $dbh-&gt;prepare("SELECT username FROM user WHERE username = :username");
    $sth_username-&gt;bindParam(":username", $username);
    $sth_email = $dbh-&gt;prepare("SELECT email FROM user WHERE email = :email");
    $sth_email-&gt;bindParam(":email", $email);
    $sth_username-&gt;execute();
    $sth_email-&gt;execute();
    if ($result = $sth_username-&gt;fetch(PDO::FETCH_OBJ)) {
        print $result-&gt;username . "The username is already is use";
    }elseif ($result = $sth_email-&gt;fetch(PDO::FETCH_OBJ)){
        print $result-&gt;email . "That e-mail is already in use";
    }else{
        $sth = $dbh-&gt;prepare("INSERT INTO user (username, password, email, salt) VALUES (:username, :password, :email, :salt)");
        $sth-&gt;bindParam(":username", $username);
        $sth-&gt;bindParam(":password", $password_hash);
        $sth-&gt;bindParam(":email", $email);
        $sth-&gt;bindParam(":salt", $salt);
        $sth-&gt;execute();
        $sth = $dbh-&gt;prepare("INSERT INTO stats (strength,agility,intelligence) VALUES (10,10,10)");
        $sth-&gt;execute();

        echo "You have been registered";
    }
}
?&gt;
Looks O.K. to me as well, as far as the SQL injection goes.

However, I'd suggest checking the return value of $sth-&gt;execute();, so you capture possible errors (i.e., DB being down).

You might also consider doing a case insensitive search for the existing usernames and e-mails, since "person@gmail.com" is the same address as "Person@GMail.com" and users "Person" and "person" are hard to distinguish.

I think empty($string) is more advisable than strlen($string) == 0.

Last, but not least, adding a "retype your password" field would be a good security against user mistyping what he wants for a password and then ending with a useless account (or having to use "I forgot my password" before the very first login).