checking the value exist in db and return alert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

checking the value exist in db and return alert

Post 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);




User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: checking the value exist in db and return alert

Post 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");
			 }
    } 

(#10850)
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

Re: checking the value exist in db and return alert

Post by newbaba »

Thanks alot
I am really grateful to this website for learning php
:)
pooja7r
Forum Newbie
Posts: 1
Joined: Fri Mar 23, 2018 8:29 am

Re: checking the value exist in db and return alert

Post 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
}
benanamen
Forum Newbie
Posts: 18
Joined: Sun Nov 15, 2015 11:57 am

Re: checking the value exist in db and return alert

Post 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.
Post Reply