Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to check my data before inserting it into the database, but this error always appear

What I have tried:

PHP
<pre>if(isset($_POST['submit'])){
    

$first_name = test_input($_POST["firstname"]);
$last_name = test_input($_POST["lastname"]);
$id = test_input($_POST["member_id"]);
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
$email = test_input($_POST["email"]);
$membertype = test_input($_POST["membertype"]);
$phone = test_input($_POST["phonenumber"]);
$department = test_input($_POST["department"]);

 
  $query = "SELECT * FROM lms WHERE  ID='".$id."' OR email='".$email."' OR  user_name='".$username."' ";
$resultdata = mysqli_query($link , $query);
$data = mysqli_fetch_array($resultdata, MYSQLI_NUM);
    
if($data[0] > 1) {
    echo "User Already in Exists<br/>";
}

else
{
    $sql = "INSERT INTO `member` (`ID`, `first_name`, `last_name`, `member_type`, `password`, `email`, `phoneNo`, `department`, `user_name`)
VALUES ('$id', '$first_name', '$last_name','$membertype','$password','$email','$phone','$department','$username')";
    if (mysqli_query($link,$sql))
    {
        echo "You are now registered<br/>";
    }
    else
    {
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);    }
}
    
    
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>


i have tried also this
PHP
$results = mysql_num_rows($resultdata);

and
PHP
$user = mysqli_fetch_assoc($resultdata);

but it gives me the same error, I know this error appears because of the query return false result, but I don't what else should I use
Posted
Updated 23-Apr-18 4:06am

1 solution

Check the return value as shown in the examples at PHP: mysqli::query - Manual[^]:
PHP
if ($resultdata = mysqli_query($link, $query)) {
    /* Success: $resultdata is a mysqli_result object */
    /* Can use $resultdata as parameter for other mysqli functions here */

    /* free result set */
    mysqli_free_result($resultdata);
} else {
    /* Failure: $resultdata is boolean false */
    /* Use mysqli_error($link) to get the error message */
}
 
Share this answer
 
Comments
Member 12214576 23-Apr-18 12:55pm    
Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900