To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Database

Database Conversation regarding PHP and SQL

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 05-15-2006, 02:47 AM   #1
essexboyracer
Member
 
Join Date: Jun 2001
Location: UK
Posts: 51
trying to avoid duplicates

Hi

I am trying to run a select statement before an IF to avoid inserting duplicate records, as follows

Code:
$surname = $item[2];
$phone = $item[4];

$result1=mysql_query("SELECT * FROM proposer WHERE proposer3 LIKE '$surname' AND proposer5 LIKE '$phone'");

if ($result1) {
   die('Duplicate entry found, stopping:' . mysql_error());

} else {


	$query = "INSERT INTO proposer SET ID = '', date = '$today', certNo = 'TEST', proposer1 = '$item[0]', proposer2 = '$item[1]', proposer3 = '$item[2]', proposer4 = '$item[3]', proposer5 = '$item[4]', proposer6 = '$item[5]', proposer7 = '$item[6]', proposer8 = '$item[7]', proposer9 = '$item[8]', proposer10 = '$item[9]', proposer11 = '$item[10]', proposer12 = '$item[11]', proposer13 = '$item[12]', proposer14 = '$item[13]'";

	mysql_query($query);
	$last_insert_id = mysql_insert_id();
	mysql_close();


	echo $last_insert_id; echo $item[0]; echo $item[1]; echo $item[2]; echo $item[4]; echo $item[8];

}
While testing I have $phone and $surname as values that I know are in the database, but it still executes the INSERT query, and deosnt stop with the die(). In addition mysql_error() doesnt give me anything.
essexboyracer is offline   Reply With Quote
Old 05-15-2006, 03:04 AM   #2
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 13,058
Just because the query was successful does not mean that there were any rows retrieved. You want to count the number of rows retrieved.
PHP Code:
$result1 = mysql_query("SELECT COUNT(*) FROM proposer WHERE proposer3='$surname' AND proposer5='$phone'");

if (
mysql_result($result1, 0) > 0) {
    die(
'Duplicate entry found, stopping:' . mysql_error());
} else {
    
// ...
}
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-15-2006, 05:00 AM   #3
edwardp
Senior Member
 
Join Date: Jul 2004
Location: Grimsby UK
Posts: 143
Not sure if my problem is similar

I m getting a similar problem.

im doing this:

PHP Code:
$query = "SELECT a.PART_ID, b.DESCRIPTION, count(a.PART_ID) as number FROM CUST_ORDER_LINE as a, PART as b WHERE a.PART_ID = b.ID AND a.PART_ID NOT LIKE '%FIN%' AND a.PART_ID NOT LIKE 'CI%' AND a.PART_ID NOT LIKE '%D&M%' AND a.PART_ID NOT LIKE 'PRECHA%' AND a.PART_ID NOT LIKE 'works van' AND a.PART_ID NOT Like '[0-9][0-9]SHT[0-9][0-9][0-9][0-9]' AND a.PART_ID NOT Like '[0-9][0-9]TIM[0-9][0-9][0-9][0-9]' GROUP BY a.PART_ID, b.DESCRIPTION ORDER BY number desc";

$dbaseConnection = connectToMSSQL();
$result = mssql_query($query,$dbaseConnection);
            
for (
$i = 0; $i < mssql_num_rows( $result ); $i++){
    
$line = mssql_fetch_array($result);
        
print_r($line);
        echo
"<br>";
}
and a few selections of results gives

Code:
Array ( [0] => 97PLU6017 [PART_ID] => 97PLU6017 [1] => Shower Tray 900 X 720 White [DESCRIPTION] => Shower Tray 900 X 720 White [2] => 351 [number] => 351 ) 
Array ( [0] => 99PLU0005 [PART_ID] => 99PLU0005 [1] => Water Filler And Locking Cap Complete [DESCRIPTION] => Water Filler And Locking Cap Complete [2] => 351 [number] => 351 ) 
Array ( [0] => 00IRN0010 [PART_ID] => 00IRN0010 [1] => Swivel Table Leg - Brown [DESCRIPTION] => Swivel Table Leg - Brown [2] => 312 [number] => 312 ) 
Array ( [0] => 99ELE2200 [PART_ID] => 99ELE2200 [1] => 13w Batten Uplighter - Ds415 [DESCRIPTION] => 13w Batten Uplighter - Ds415 [2] => 237 [number] => 237 ) 
Array ( [0] => 00MAS1011 [PART_ID] => 00MAS1011 [1] => 2001/2 Frt/Chy SideTape20/45/45/20mm Pe [DESCRIPTION] => 2001/2 Frt/Chy SideTape20/45/45/20mm Pe [2] => 177 [number] => 177 ) 
Array ( [0] => AFT03WIN0002 [PART_ID] => AFT03WIN0002 [1] => Inner Lock Assy, [DESCRIPTION] => Inner Lock Assy, [2] => 162 [number] => 162 )
As you may see, in each array (line) i am getting a double results...one with integer indexes and another with assoc indexes. I would like only assoc. Is this usual or am missing soething in my code??

regards
__________________
EdwardP

Pain is weakness leaving the body
edwardp is offline   Reply With Quote
Old 05-15-2006, 05:14 AM   #4
edwardp
Senior Member
 
Join Date: Jul 2004
Location: Grimsby UK
Posts: 143
Resolved

This i changed the line with $line = mssql_fetch_array($result) to $line= mssql_fetch_array($result,MSSQL_ASSOC) na it worked beautifully
__________________
EdwardP

Pain is weakness leaving the body
edwardp is offline   Reply With Quote
Old 05-15-2006, 06:29 AM   #5
essexboyracer
Member
 
Join Date: Jun 2001
Location: UK
Posts: 51
Thank sLaserlight, I dont fully understand the difference between you explaination and !$return
essexboyracer is offline   Reply With Quote
Old 05-15-2006, 03:30 PM   #6
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,067
WHOA! Hold on! None of these ways are how you avoid duplicates, because they're all vulnerable to race conditions. I.e. someone can insert the same thing between when you select for dupes and insert.

You avoid duplicates with unique indexes. Since a unique index on a large text field is large and unwieldy, it's an easy cheat to do something like create a separate field, and put and md5 of the text field in there, and make the unique index on that field.

create table posts (id int autoincrement, textarea text, checksum text);
$text = "this is a test message";
insert into posts (textarea, checksum) values ('$text',md5sum($text));

Get the idea? If they try to insert the same thing twice, the unique index on the md5checksum should catch it every time, no race condition.
__________________
PostgreSQL version 8.5 is now in alpha!
Sxooter is offline   Reply With Quote
Old 05-15-2006, 05:30 PM   #7
essexboyracer
Member
 
Join Date: Jun 2001
Location: UK
Posts: 51
eh? my eye is startin to twitch and I am murmering with dribble down the side of my mouth

whats race got to do with it...
essexboyracer is offline   Reply With Quote
Old 05-15-2006, 05:44 PM   #8
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,067
Race conditions have nothing to do with the color of your skin...

T1 us transaction 1, T2 is transaction 2.

T1: select count(*) from db where message = 'abc'; <-- abc not in database, count==0
T2: select count(*) from db where message = 'abc'; <-- abc not in database, count==0

milliseconds pass...

T1: insert into db values ('abc');
T2: insert into db values ('abc');
__________________
PostgreSQL version 8.5 is now in alpha!
Sxooter is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:36 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.