rsammy Posted August 17, 2006 Share Posted August 17, 2006 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/ Share on other sites More sharing options...
AndyB Posted August 17, 2006 Share Posted August 17, 2006 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 Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76471 Share on other sites More sharing options...
appeland Posted August 17, 2006 Share Posted August 17, 2006 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 USto understand why there is a paranoia about a little number :) Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76473 Share on other sites More sharing options...
rsammy Posted August 17, 2006 Author Share Posted August 17, 2006 [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! Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76474 Share on other sites More sharing options...
rsammy Posted August 17, 2006 Author Share Posted August 17, 2006 [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 USto 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. Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76475 Share on other sites More sharing options...
tomfmason Posted August 17, 2006 Share Posted August 17, 2006 You can use [url=http://us2.php.net/manual/en/function.preg-match.php]preg_match[/url] to make sure that it contains numbers and no letters. Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76485 Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 To check for duplicates just run a SELECT statement to see if a record allready exists with that SSN. Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76506 Share on other sites More sharing options...
HeyRay2 Posted August 17, 2006 Share Posted August 17, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76515 Share on other sites More sharing options...
rsammy Posted August 18, 2006 Author Share Posted August 18, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76894 Share on other sites More sharing options...
tomfmason Posted August 18, 2006 Share Posted August 18, 2006 You can do something like this to view the users data.[code]<?phpsession_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 Quote Link to comment https://forums.phpfreaks.com/topic/17889-validate-ssn/#findComment-76977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.