Jump to content


Photo

Data Checking


  • Please log in to reply
2 replies to this topic

#1 bschultz

bschultz
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts
  • LocationMinnesota, USA

Posted 31 March 2006 - 01:20 AM

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.

<? 
$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 kitchen
VALUES('$_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 (); 
?>  


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 31 March 2006 - 04:29 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 bschultz

bschultz
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts
  • LocationMinnesota, USA

Posted 31 March 2006 - 08:12 PM

[!--quoteo(post=360258:date=Mar 31 2006, 12:29 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Mar 31 2006, 12:29 AM) View Post[/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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users