Jump to content


Photo

Check for duplicates in mySQL using PHP


  • Please log in to reply
9 replies to this topic

#1 chasdrury

chasdrury
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 September 2006 - 10:15 AM

Hi guys

I have created the following PHP script which works perfectly - sends name, phone and email to a mySQL database.

What I need to do it somewhere put in a command that checks the Database for duplicate email entries - The email sends out discount details for the shop so we only want to send one per email address. Is this possible? How would I go about doing this?

Cheers

Chas

$name=$_POST['requiredname'];
$phone=$_POST['requiredphone'];
$email=$_POST['requiredemail'];
$from = "info@hairkandi.com";
$subject = "Hair Kandi Discount Voucher";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$name','$phone','$email')";
mysql_query($query);

    //begin message
    $message = <<<EOF
<html>
  <body bgcolor="#333333">


#2 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 29 September 2006 - 10:35 AM

it's been a long time since I've done any PHP coding so if it doesn't work... sorry.

$check = mysql_query("SELECT email FROM contacts");
$result = mysql_fetch_array($check);

foreach ($result as $i)
{
	if ($i == $_POST['requiredemail'])
	{
		exit("dupe");
	}
}

what this *should* do is select all the "email" fields from "contacts" and store them in a new array.

then the foreach() loops through them one by one and check them against $_POST['requiredemail'].

it's a $i matches $_POST['requiredemail'] it will exit().

you can tidy this up a bit so it could print an error message or something.

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 September 2006 - 10:39 AM

This query will tell you which email addys are duplicated
SELECT email, COUNT(*) as adcount
FROM contacts
GROUP BY email
HAVING adcount > 1

Once you have got rid of the duplicates, put a unique index on email column to prevent further duplication
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 chasdrury

chasdrury
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 September 2006 - 10:44 AM

it's been a long time since I've done any PHP coding so if it doesn't work... sorry.

$check = mysql_query("SELECT email FROM contacts");
$result = mysql_fetch_array($check);

foreach ($result as $i)
{
	if ($i == $_POST['requiredemail'])
	{
		exit("dupe");
	}
}

what this *should* do is select all the "email" fields from "contacts" and store them in a new array.

then the foreach() loops through them one by one and check them against $_POST['requiredemail'].

it's a $i matches $_POST['requiredemail'] it will exit().

you can tidy this up a bit so it could print an error message or something.


Thanks. Where would the code go in my PHP then, just with all the other code?

The database is currently empty - what I want to do is stop duplicates from appearing. Is this how to do it?

Chaz

#5 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 29 September 2006 - 10:47 AM

if your DB is empty I would use Barand's example and set unique key in your DB.

if you want to use my code then you should put it somewhere before you send the message or add anything to your DB.

no point error checking afterwards is it  ;)

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 September 2006 - 10:57 AM

Chris9902,

Afraid that code won't work. It fetches all email address but only checks the first one.

Better to look for the specific email address being entered to see if any there already
<?php
$check = mysql_query("SELECT COUNT(*) FROM contacts WHERE email = '$email' ");
if (mysql_result ($check, 0) > 0) {
     // error - email exists
}
else {
     // insert code can go here
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 29 September 2006 - 11:00 AM

;) told you it had been a long time.

man I need some books.

#8 chasdrury

chasdrury
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 September 2006 - 11:22 AM

Thanks guys. I have inputted this into the insert.php script and it still doesn't work. It lets through any email, whether or not they are in the database. The only difference now is that I have put a unique identifier in the email list in the SQL, so the data isn't written to the database, but the people still get the auto email discount thing, since no error is kicked up.

My PHP is below (I have copied in everything upto the start of the discount email)

<?
$username="xxx";
$password="xxx";
$database="xxx";
$name=$_POST['requiredname'];
$phone=$_POST['requiredphone'];
$email=$_POST['requiredemail'];
$from = "info@hairkandi.com";
$subject = "Hair Kandi Discount Voucher";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$check = mysql_query("SELECT COUNT(*) FROM contacts WHERE email = '$email' ");
if (mysql_result ($check, 0) > 0) {
     // error - email exists
}
else {
}

$query = "INSERT INTO contacts VALUES ('','$name','$phone','$email')";
mysql_query($query);

    //begin message
    $message = <<<EOF
<html>
  <body bgcolor="#333333">
<table width="511" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="511" height="113" valign="top"><img src=

What am i doing wrong!!!??

Chaz

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 29 September 2006 - 11:45 AM

else {
}

The processing (if no dupes) needs to be inside the else {}

<?php

if (mysql_result ($check, 0) > 0) {
     // error - email exists
     echo "Email exists in file<br/>";
}
else {
    $query = "INSERT INTO contacts VALUES ('','$name','$phone','$email')";
    mysql_query($query);

        //begin message
        $message = <<<EOF
    <html>
      <body bgcolor="#333333">
    <table width="511" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr> 
        <td width="511" height="113" valign="top"><img src=
        
        ...
        ...
}


?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 chasdrury

chasdrury
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 September 2006 - 11:59 AM

Thanks very much, works perfectly now!

Chaz




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users