0

There are no errors shown when I click the register button to register new users, but the data isn't inserted in the database.

<?php
    session_start();
     if(isset($_POST['name'])&&isset($_POST['password'])){
    $name=$_POST['name'];
    $password=$_POST['password'];

    $link=mysqli_connect("localhost","root","");
    $select=mysqli_select_db($link,'first_db');
    $name=mysqli_real_escape_string($link,$name);
    $password=mysqli_real_escape_string($link,$password);
    $query="SELECT * FROM users WHERE name='$name'";
    $run=mysqli_query($link,$query);    
    $count = mysqli_num_rows($run);

    if ($count>0) {
    echo 'Sorry! This Username already exists!';
    } else {
        $name = $_POST['name'];
        $password=$_POST['password'];
        $sql = "INSERT INTO users (name, password)
                VALUES
                ('$_POST[name]','$_POST[password]')";
    }
     }
    else{
        echo"Cannot be blank";
    }
?>
4
  • Save $_POST[name] to a variable first, $name = $_POST[name] and $password = $_POST[password]. Then put it inside your sql statement. Like, $sql = "..... values ('$name', '$password')"; Commented Dec 5, 2017 at 4:45
  • Thank you Erfan Ahmed Emon it worked Commented Dec 5, 2017 at 4:49
  • @user9053914 Use Prepared Queries to enhance your security. Commented Dec 5, 2017 at 4:52
  • Don't use the deprecated and insecure mysql*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7 (in 2015). Use MySQLi or PDO instead. 2. You are wide open to SQL Injections and should really use Prepared Statements instead of concatenating your queries, which can be used if you use the above mentioned MySQLi or PDO. Commented Dec 5, 2017 at 4:55

3 Answers 3

2

check your else part you just created the query need to execute it

if ($count>0)
{
   echo 'Sorry! This Username already exists!';
} 
else
{
    $name = $_POST['name'];
    $password=$_POST['password'];
    $sql = "INSERT INTO users (name, password)VALUES('$name','$password')";
    $run=mysqli_query($link,$sql);// add this statement then your record inserted



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

Comments

1

You forgot to execute your Insert query like below :

mysqli_query($link,$sql); 

But you have to use Prepared Insert Query to make it more secure:

// prepare and bind User Query
$queryUsers = $conn->prepare("INSERT INTO 
users(name,password) VALUES (?, ?)");
$queryUsers->bind_param("ss",$name,$password);

$name = $_POST[name];
$password = $_POST[password];

// execute Users Query
$queryUsers->execute();

// Close Connections
$queryUsers->close();

Comments

0

Just need to execute the query string like you did to check whether the user already exists or not.

$sql = "INSERT INTO users (name, password)
            VALUES
            ('$_POST[name]','$_POST[password]')";
$run=mysqli_query($link,$sql);    // Executing the query here.

Comments

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.