Jump to content

Preventing duplicate entries


deebler

Recommended Posts

Hi - here is my php code for my Mysql table:

 

<?php

 

$con = mysql_connect("database");

if (!$con)

{

  die('Could not connect: ' . mysql_error());

  }

$sql="INSERT INTO database (id, name, address, city, state, zip, cell, email,info)

VALUES

('','$_POST[name]','$_POST[address]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[cell]','$_POST','$_POST[info]')";

mysql_select_db("tampa", $con);

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  }

 

mysql_close();

 

echo header ( "Location: /cc-common/contests"); // Your code here to handle a successful verification 

  //}; 

?>

 

 

My question is what line of code can I put in to prevent duplicate emails AND addresses from being added to the database?

 

I'm new to php and trying to figure it all out.

 

Thanks!

Link to comment
Share on other sites

your code needs revising

 

<?php

$con = mysql_connect("host","username","password"); //fill in your data
if (!$con)
{
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("database_name"); //insert appropriate data
//declare variables
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$cell = $_POST['cell'];
$email = $_POST['email'];
$info = $_POST['info'];
$result = mysql_query("SELECT * FROM  database WHERE email = '$email' AND address = '$address'") or exit(mysql_error()); //check for duplicates
$num_rows = mysql_num_rows($result); //number of rows where duplicates exist
if($num_rows == 0) { //if there are no duplicates...insert
$sql="INSERT INTO database (id, name, address, city, state, zip, cell, email,info)
VALUES
('','$_POST[name]','$_POST[address]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[cell]','$_POST[email]','$_POST[info]')";   

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
}

mysql_close();

header ("Location: /cc-common/contests"); // Your code here to handle a successful verification   
         //};         
?>

Link to comment
Share on other sites

lets change the AND to an OR

 

$result = mysql_query("SELECT * FROM  database WHERE email = '$email' OR address = '$address'") or exit(mysql_error()); //check for duplicates

 

I don't see how that would fail

Link to comment
Share on other sites

well the AND would only work if you tried to submit the same address AND email, if you tried to submit the same email but a different address, it would still work....my error sorry about that..fixed now

Link to comment
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.