-1

I Cannot Check whether the username already exist in database. I gone through existing questions that were answered here. None of them solved my problem. When i executes, it displays "Cannot select username from table", which i given inside die block. Code Is given below.

<?php
    $username = $_POST['user_name'];
    $password = $_POST['pass_word'];

    $host = "localhost";
    $db_username = "root";
    $db_password = "";
    $db_name = "my_db";

    //create connection
    $conn = @new mysqli($host, $db_username, $db_password, $db_name);

    if (isset($_POST["submit"]))
    {
        # code...

        //check  connection established or not
        if ($conn->connect_error)
        {
            die("Not Connected to DB"); 
        }
        else
        {
            $query = "SELECT 'usernamedb' FROM 'registration' WHERE usernamedb='$username'";
            $result = mysqli_query($conn, $query) or die('Cannot select username from table');
            if (mysqli_num_rows($result)>0)
            {
                $msg.="This username already exist. try Another !!";
            }
            else
            {
                $insert = "INSERT INTO 'registration'('id', 'usernamedb', 'password') VALUES ([$username],[$password])";
                $insert_result = mysqli_query($conn,$insert) or die('INSERTION ERROR');
            }
        }
        $conn->close();
    }
?>

Hope someone will answer me.

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
  • 2
    Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – Nigel Ren Jun 30 '18 at 13:55
  • You have an error in your sql syntax. Instead of using quotes `'usernamedb'` you should use nothing, or the backtick symbol `\`usernamedb\`` – Michael Beeson Jun 30 '18 at 13:55
  • 2
    Avoid using `@` as this also covers any errors that may be significant. – Nigel Ren Jun 30 '18 at 13:56

1 Answers1

0

First of all you should not use those unescaped queries.

But regarding your question you have an SQL error on your queries. You quoted table name. "FROM 'registration'" should be "FROM registration".

  • my problem solved, when i removed unnecessary quotes. I have one more question. if i need to display the "this user already registered" on same page. What i should do ? – Mohammed Faris.K Jul 02 '18 at 17:26