Please anyone can help me.

I need to add an if statement to verify if a user already exists on a table. Can someone help me on this. Here is my code. The code below it work but it doesn't verifyed if a user already exists.

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "password", "database");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());

// Escape user inputs for security
$UserName = mysqli_real_escape_string($link, $_POST['UserName']);
$temppass = mysqli_real_escape_string($link, $_POST['temppass']);

// attempt insert query execution
$sql = "INSERT INTO user (UserName, temppass) VALUES ('$UserName', '$temppass')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

// close connection

If the username is unique (which it should be) you can do a SELECT first with the username as the parameter. If the result contains a row the user already exists, if it's empty then the user is not already in the table.
You could also execute the insert if the username column is set to be unique and if the insert fails you know it probably failed because the users exists. But the first way is better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.