bschultz Posted March 31, 2006 Share Posted March 31, 2006 I have the following code to insert data into a database. I'd like to add some data checking so that if the email address is already in the database, it won't let you add another record. The problem is, I don't know where to start. Can somebody point me in the right direction? Thanks.[code]<? $DBhost = "xxx"; $DBuser = "xxx";$DBpass = "xxx";$DBName = "kitchen";mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); @mysql_select_db("$DBName") or die("Unable to select database $DBName"); $sqlquery = "INSERT INTO kitchenVALUES('$_POST[name]', '$_POST[address]', '$_POST[city]', '$_POST[state]','$_POST[phone]', '$_POST[email]', '$_POST[age]')";$results = mysql_query($sqlquery); if ($results){ echo "<p>Thank you, your entry has been submitted!</p>"; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/6228-data-checking/ Share on other sites More sharing options...
fenway Posted March 31, 2006 Share Posted March 31, 2006 There are a number of ways to handle this. You can do the check yourself before hand, and see if you match any existing records (but this isn't transaction-safe). Alternatively, you can add a UNIQUE index of the column, and see if the INSERT fails. Or you could use INSERT IGNORE, and see if any records where added, and inform the user accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/6228-data-checking/#findComment-22535 Share on other sites More sharing options...
bschultz Posted March 31, 2006 Author Share Posted March 31, 2006 [!--quoteo(post=360258:date=Mar 31 2006, 12:29 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Mar 31 2006, 12:29 AM) [snapback]360258[/snapback][/div][div class=\'quotemain\'][!--quotec--]Alternatively, you can add a UNIQUE index of the column, and see if the INSERT fails. [/quote]That did the trick (didn't even know that feature existed!)...thanks! Quote Link to comment https://forums.phpfreaks.com/topic/6228-data-checking/#findComment-22740 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.