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
https://forums.phpfreaks.com/topic/241425-preventing-duplicate-entries/
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   
         //};         
?>

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.