Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8441639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:36:54+00:00 2026-06-10T08:36:54+00:00

Possible Duplicate: Prevent Duplicate SQL entries I’m just looking for a simple way to

  • 0

Possible Duplicate:
Prevent Duplicate SQL entries

I’m just looking for a simple way to tell me if a record already exists, and if it does, not to insert it into the table. The below code inserts regardless if it exists or not… and I just cannot figure out why.

<?php
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];

$conn = sqlsrv_connect( $hostname, $connectionInfo)

$dup = sqlsrv_query($conn, "SELECT * FROM contact WHERE (FirstName='$FirstName') AND (LastName='$LastName')");
if(sqlsrv_num_rows($dup) > 0)
{
    echo "Already Exists";
}
else
{
    $query = "INSERT INTO contact (FirstName, LastName) VALUES ('$FirstName', '$LastName')";
    sqlsrv_query( $conn, $query );
}

EDIT:

Ultimately, changing sqlsrv_num_rows($dup) > 0 to sqlsrv_has_rows($dup) fixed the problem. Below is my updated code:

$params = array($_POST['FirstName'], $_POST['LastName']);
$conn = sqlsrv_connect( $hostname, $connectionInfo)
$sql = "SELECT * FROM contact WHERE FirstName = ? AND LastName = ?";
$dup = sqlsrv_query($conn, $sql, $params);
if(sqlsrv_has_rows($dup)) 
{
    echo "Already Exists";
}
else
{
    $query = "INSERT INTO contact (FirstName, LastName) VALUES (?, ?)";
    sqlsrv_query( $conn, $query, $params );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:36:56+00:00Added an answer on June 10, 2026 at 8:36 am

    First, you need to identify your primary key. This is usually some sort of ID field. I’d probably recommend against using the pairing of FirstName LastName because there are many, many instances of people with the same first name and last name. “Kevin Kline” is the name of both an actor and a SQL Server MVP (and there are probably many, many more).

    If you want to just blindly insert FirstName LastName into a table, then a RDBMS-agnostic query would be more like

    INSERT INTO contact (FirstName, LastName) VALUES ($FirstName, $LastName) WHERE NOT EXISTS 
    (SELECT 1 FROM contact where FirstName = $FirstName and LastName = $LastName);
    

    (disclaimer: I know I didn’t format it according to what you had in your post, that is due to the following)

    HOWEVER:

    Your code is also vulnerable to SQL injection attacks. Please read up on the PHP method sqlsrv_query paying special attention to the “params” argument.

    Additionally, going back to the “Kevin Kline” example, this will only insert the first “Kevin Kline.” I suppose this is fine if you’re adding “Kevin Kline” as a dimension in a star schema that will be associated with other dimensions like Profession in a fact table. However, if that’s not your application domain, then I’d highly recommend that you determine what you’re going to use as your table keys so that you can accurately track the appropriate data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to prevent SQL injection? For logging in: $username = mysql_real_escape_string(htmlspecialchars(strip_tags(trim($_POST['username'])),
Possible Duplicate: Best way to prevent SQL Injection in PHP On my site I
Possible Duplicate: Best way to prevent SQL Injection in PHP The necessity of hiding
Possible Duplicate: Best way to prevent SQL Injection in PHP Is this code protected
Possible Duplicate: Best way to prevent SQL Injection in PHP I'm trying to insert
Possible Duplicate: Best way to prevent SQL Injection in PHP Is this code secure
Possible Duplicate: Best way to prevent SQL Injection in PHP What is the best
Possible Duplicate: Best way to prevent SQL Injection in PHP In the code below,
Possible Duplicate: Best way to prevent SQL Injection in PHP I'm new to PHP.
Possible Duplicate: Is there a clean way to prevent windows.h from creating a near

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.