Jump to content

Data Checking


bschultz

Recommended Posts

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 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 ();
?>  
[/code]
Link to comment
https://forums.phpfreaks.com/topic/6228-data-checking/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/6228-data-checking/#findComment-22535
Share on other sites

[!--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!
Link to comment
https://forums.phpfreaks.com/topic/6228-data-checking/#findComment-22740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.