User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,310 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,286 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 965 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 25
Reputation: chicago1985 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
chicago1985 chicago1985 is offline Offline
Light Poster

Prevent duplicate entry

  #1  
Dec 20th, 2007
I have my PHP inserting into Oracle 9i.
But how do I prevent duplicate record entries?

I only have 3 fields in the insert in the action page and my below attempt doesnt output anything or insert anything even if not a duplicate:

  1. <?php
  2. $c=OCILogon("scott", "tiger", "orcl");
  3. if ( ! $c ) {
  4. echo "Unable to connect: " . var_dump( OCIError() );
  5. die();
  6. }
  7. $mydupcheck = "select * from personTable where firstname = $_POST[firstname] and lastname = $_POST[lastname] and emailaddress = $_POST[emailaddress]";
  8.  
  9. if($mydupcheck)
  10. {
  11. echo "Duplicate entry";
  12. }
  13. else
  14. {
  15. $s = OCIParse($c, "INSERT INTO personTable (firstname,lastname,emailaddress) VALUES ($_POST[firstname],'$_POST[lastname]','$_POST[emailaddress]'");
  16. OCIExecute($s, OCI_DEFAULT);
  17. echo "Record successfully entered";
  18. }
  19. ?>


How do I prevent duplicate record info and show the user they entered duplicate info.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: Prevent duplicate entry

  #2  
Dec 20th, 2007
The best approach is to have the DB enforce Referenctial Integrity. Basically you instruct the database at design time NOT to allow duplicates for certain records ever. IF such an attempt is made, the db server will not carry out the operation. Look at these:

http://www.google.com/search?hl=en&c...Oracle&spell=1
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: Prevent duplicate entry

  #3  
Dec 20th, 2007
The "hard" (and wrong) approach would be to first query the database to check for the existence of the record and insert it only if no record exists. But the right way to approach this is through referential integrity.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Prevent duplicate entry

  #4  
Dec 21st, 2007
Originally Posted by chicago1985 View Post
I have my PHP inserting into Oracle 9i.
But how do I prevent duplicate record entries?

I only have 3 fields in the insert in the action page and my below attempt doesnt output anything or insert anything even if not a duplicate:

There are some errors in your script.
$mydupcheck = "select * from personTable where firstname = $_POST[firstname] and lastname = $_POST[lastname] and emailaddress = $_POST[emailaddress]";

Since firstname, lastname and emailaddress are strings, you should put them in quotes. And btw, you are not executing that query.

if($mydupcheck)
{
echo "Duplicate entry";
}
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: Prevent duplicate entry

  #5  
Dec 23rd, 2007
Try this:
<?php
$c=OCILogon("scott", "tiger", "orcl");
  if ( ! $c ) {
    echo "Unable to connect: " . var_dump( OCIError() );
    die();
  }

$sqlString = "select * from personTable where firstname='" . $_POST['firstname'] . "' and lastname='" . $_POST['lastname'] . "' and emailaddress='" . $_POST['emailaddress'] ."'");
$mydupcheck = OCIParse($c, $sqlString);

OCIExecute($mydupcheck);


$nrows = oci_fetch_all($mydupcheck, $results, 0, 1);


if($nrows)
{
   echo "Duplicate entry";
}
else
{
	$sqlString="INSERT INTO personTable (firstname,lastname,emailaddress) VALUES ('" . $_POST['firstname'] . "','" . $_POST['lastname'] . "','" . $_POST['emailaddress'] . "'";
	$s = OCIParse($c, $sqlString);
	OCIExecute($s, OCI_DEFAULT);
	echo "Record successfully entered";
}
?>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC