Page 1 of 1

checking the value exist in db and return alert

Posted: Wed Mar 07, 2018 9:18 am
by newbaba
i am facing this problem on checking the values exists in the database through the if condition
but whatever the value i enter the first statement execute
kindly guide me how to resolve it

Code: Select all

$nic= mysqli_real_escape_string($link, $_POST['nic']);
			$contact= mysqli_real_escape_string($link, $_POST['contact']);
			$company= mysqli_real_escape_string($link, $_POST['company']);
			
			
			
			if("SELECT * FROM userdata WHERE nic = '".$_POST['nic']."'")
			{
			echo("Same employee details has already been saved"));
			}
		else
			{
		
			 $sql = "INSERT INTO userdata (first_name,last_name,email_address,age,gender,degree,university,otheruniversity,address,nic,contact,company)
	VALUES ('$first_name','$last_name','$email_address','$age','$gender','$degree','$university','$otheruniversity','$address','$nic','$contact','$company' )";
		
			 if(mysqli_query($link, $sql))
			 {

			 $_SESSION['id'] = mysqli_insert_id($link);
			 
      header("location: quizenglish.php");
			 }
    } 
       mysqli_close($link);





Re: checking the value exist in db and return alert

Posted: Wed Mar 07, 2018 6:38 pm
by Christopher
You are not doing a query for the first check.

Code: Select all

$nic= mysqli_real_escape_string($link, $_POST['nic']);
			$contact= mysqli_real_escape_string($link, $_POST['contact']);
			$company= mysqli_real_escape_string($link, $_POST['company']);
			
			$sql = "SELECT * FROM userdata WHERE nic = '$nic'";
			$result = mysqli_query($link, $sql)
			
			if($result->num_rows)
			{
			echo("Same employee details has already been saved"));
			}
		else
			{
		
			 $sql = "INSERT INTO userdata (first_name,last_name,email_address,age,gender,degree,university,otheruniversity,address,nic,contact,company)
	VALUES ('$first_name','$last_name','$email_address','$age','$gender','$degree','$university','$otheruniversity','$address','$nic','$contact','$company' )";
		
			 if(mysqli_query($link, $sql))
			 {

			 $_SESSION['id'] = mysqli_insert_id($link);
			 
      header("location: quizenglish.php");
			 }
    } 


Re: checking the value exist in db and return alert

Posted: Mon Mar 19, 2018 12:21 am
by newbaba
Thanks alot
I am really grateful to this website for learning php
:)

Re: checking the value exist in db and return alert

Posted: Fri Mar 23, 2018 8:32 am
by pooja7r
$user_id = 121;

$checkUserID = mysql_query("SELECT UserID from submissions WHERE UserID = '$user_id'");

if (!$checkUserID) {
die('Query failed to execute for some reason');
}

if (mysql_num_rows($checkUserId) > 0) {
echo "User id exists already.";
$user = mysql_fetch_array($checkUserId);
print_r($user); // the data returned from the query
}

Re: checking the value exist in db and return alert

Posted: Sat Mar 24, 2018 1:03 pm
by benanamen
The logic is wrong. You are creating a Race Condition. Do not check if a user already exists. Set a unique constraint on the appropriate field(s), attempt the insert and capture the duplicate error if any.