0

im doing registration and i want to check if username is used or not, dont know why mysql_num_rows(); function won't post anything

<?php
if(isset($_POST['name_signup'])) {
    if($_POST['pass_signup']==$_POST['pass_a_signup']){
        $select=mysql_num_rows(mysql_query("SELECT * FROM users WHERE nick='".$_POST['nick_signup']."'"));
        if($select=!0){
        $name=$_POST['name_signup'];
        $secondname=$_POST['secondname_signup'];
        $nick=$_POST['nick_signup'];
        $pass=$_POST['pass_signup'];
        $mail=$_POST['email_signup'];
        $passx=md5(md5($pass));
        include 'db.php';
        mysql_query("INSERT INTO users(firstname,secondname,nick,pass,email,date)VALUE('".$name."','".$secondname."','".$nick."','".$passx."','".$mail."',NOW())");
        echo "Succesfully Signed up";
        echo'<meta http-equiv="refresh" content="3;URL=../index.php">';
        }else {
            echo "Username used";
            echo'<meta http-equiv="refresh" content="3;URL=../index.php?page=signup">';
            }
    }
    else{
        echo'Passwords do not match';
        echo'<meta http-equiv="refresh" content="1;URL=../index.php?page=signup">';
        }
}

else {
    echo'<meta http-equiv="refresh" content="0;URL=../index.php">';
}
?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    Where is your database connection??? – Saty Apr 22 '16 at 11:46
  • 6
    You shouldn't use `mysql_*` functions, they are deprecated and aren't working anymore since PHP7, use `mysqli_*` or PDO instead. – Aurel Apr 22 '16 at 11:46
  • 2
    `mysql_*` is deprecated from `php 5` onward and removed completely from `php7`. please switch to `mysqli` or `PDO` – Alive to die - Anant Apr 22 '16 at 11:47
  • 1
    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). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Apr 22 '16 at 12:52
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 22 '16 at 12:52
  • Can you post the markup for your form? – Jay Blanchard Apr 22 '16 at 12:52

2 Answers2

1

The mysql extension is deprecated and you should stop using it. Instead try mysqli or PDO.

There is a bug in your code right here:

if($select=!0){

You should correct it like this:

if($select!=0){

Again, stop using mysql. And start using prepared statements or your code is in danger of SQL Injection

dimlucas
  • 5,040
  • 7
  • 37
  • 54
0

Please don't use mysql_() use mysqli_() as mysql_*() is depreciated

Update this if($select=!0){ to "if($select > 0){" or

$qry = mysql_query("SELECT * FROM users WHERE nick= '".$_POST['nick_signup']."' ");
$select=mysql_num_rows(
$qry);
        if($select > 0){
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14