PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jun 19, 2018 10:45 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Mar 07, 2018 10:18 am 
Offline
Forum Newbie

Joined: Thu Jan 26, 2017 11:36 pm
Posts: 17
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

Syntax: [ Download ] [ Hide ]
$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);




 


Top
 Profile  
 
PostPosted: Wed Mar 07, 2018 7:38 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13561
Location: New York, NY, US
You are not doing a query for the first check.
Syntax: [ Download ] [ Hide ]
$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)


Top
 Profile  
 
PostPosted: Mon Mar 19, 2018 12:21 am 
Offline
Forum Newbie

Joined: Thu Jan 26, 2017 11:36 pm
Posts: 17
Thanks alot
I am really grateful to this website for learning php
:)


Top
 Profile  
 
PostPosted: Fri Mar 23, 2018 8:32 am 
Offline
Forum Newbie

Joined: Fri Mar 23, 2018 8:29 am
Posts: 1
$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
}


Top
 Profile  
 
PostPosted: Sat Mar 24, 2018 1:03 pm 
Offline
Forum Newbie

Joined: Sun Nov 15, 2015 12:57 pm
Posts: 18
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group