Jump to content

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

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.