Jump to content

validate SSN


rsammy

Recommended Posts

hi,

how do i validate SSN using PHP? i mean, i have a SSN field in my MySQL database. I am validating it for lenght(9 digits) and numerics! but, i need to validate it for duplicates too. how do i prevent duplicates?

the application i am working on is:

i can enter a patient into the system with a temp id(SSN like PXXXXXXXXXXX). The doctors keep checking on the patient. But the Data Entry associate at the front office(or whoever that is) will need to update the table with the patients proper SSN. the associate enters a SSN and the system validates it for duplicates. If no match is found, the new SSN is updated for the record. If SSN exists, a new screen is thrown up asking if new patient info needs to overwrite existing info or ignore. If associate says YES to overwrite, then database shud be updated with new SSN and other info for existing rec!

hope i didnt confuse u guys ???
Link to comment
Share on other sites

Before you go too far, you might want to be sure that privacy policies/legislation actually allow you to ask for an SSN.  Although people might ask for mine, my answer is always "that's between me and the government, and it's private".
Link to comment
Share on other sites

Well, you are going to write it to some database somewhere, I guess.
Just do a select on the column, checking if the same one is already in there.

For me personally that's just a number and I guess you have to be resident of the US
to understand why there is a paranoia about a little number :)
Link to comment
Share on other sites

[quote author=AndyB link=topic=104680.msg417677#msg417677 date=1155849698]
Before you go too far, you might want to be sure that privacy policies/legislation actually allow you to ask for an SSN.  Although people might ask for mine, my answer is always "that's between me and the government, and it's private".
[/quote]

i am working on a medical application for a group of doctors. ssn needs to be collected and entered into the system for the patient!
Link to comment
Share on other sites

[quote author=appeland link=topic=104680.msg417679#msg417679 date=1155850233]
Well, you are going to write it to some database somewhere, I guess.
Just do a select on the column, checking if the same one is already in there.

For me personally that's just a number and I guess you have to be resident of the US
to understand why there is a paranoia about a little number :)
[/quote]

i am in the US myself and do have an SSN! i need to validate this field. Not sure how to do it.
Link to comment
Share on other sites

Something like this will get you started (you'll need add / update confirmation logic and such):

[code]
<?php

$SSN = "123-45-6789";

if (preg_match("/^\d{3}-\d{2}-\d{4}$/", $SSN)) {
  // SSN is valid

  // Check if SSN already in database
  $sql = "SELECT COUNT(*) as ssn_match FROM patient_table WHERE ssn = '$SSN'";
  $result = mysql_query($sql) or die("Error querying SSN: ".mysql_error());
  $row = mysql_fetch_row($result);

  if($row['ssn_match'] == 0){
// No matches, add patient as new
$sql_new = "INSERT INTO patient_table SET ssn = '$SSN', other_field = 'other_value'";
mysql_query($sql_new) or die("Error adding patient: ".mysql_error());
  } else {
// Match found, update current patient
$sql_update = "UPDATE patient_table SET other_field = 'other_value' WHERE ssn = '$SSN' LIMIT 1";
mysql_query($sql_update ) or die("Error updating patient: ".mysql_error());
  }

} else {
  // SSN is invalid, show error
  echo "SSN must be in the format XXX-XX-XXXX (where X is a number 0 - 9)";
}

?>
[/code]
Link to comment
Share on other sites

thanks, for ur replies. it helped me quite a bit.

when i check for ssn and thenfind a match, i need to show a new screen with details of the existing ssn(name, address, date of admission, etc.) and ask user if he/she wishes to overwrite existing data with new data or retian old data!

how can i do that?

my exisitign code is shown in the [b]attached file[/b].


i am not updating the database table in this php file. the actual updates/inserts are done in anothoer page with confirmation message! I need to add another screen between these two php pages. thats where i present users with the option to either over write or retain existing data for a duplicate ssn entered.

i hope my issue is clear to u guys
Link to comment
Share on other sites

You can do something like this to view the users data.

[code]
<?php
session_start();
//your session stuff
$sql = mysql_query("SELECT * FROM `yourtable` WHERE `ssn` = '$ssn'") or die(mysql_error());

while ($rw = mysql_fetch_assoc($sql)) {
    // now you can ether echo the users details or set it into variables
    echo 'Welocome ' . $rw['firstname'] . ' the date you joined is ' . $rw['date_added'] . '';
    //or
    $first_name = $rw['first_name'];
    $date_added = $rw['first_name'];
    //or you can place it in session variables
    $_SESSION['first_name'] = $rw['first_name'];
    $_SESSION['date_added'] = $rw['date_added'];
}
?>[/code]

Hope this helps,
TOm   
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.