By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Avoid duplicate entries

P: 18
Moderator: thread has been split from http://bytes.com/forum/thread833623.html.

_________________________________________________


Hello Dhiru1009, Marus, Coolsti & Nomad

I am new to this site as of just now, and ran across it while I was doing a search. And it's one of the most "recent" (as for posting dates ) I have run across, so I would suspect people still visit this forum.

This is going to the snippet I need to use, (post above) but I am fairly new to php...and can work my way around existing code...and can do small routines.

I am "cheating"...(laugh) and am using :

http://sourceforge.net/docman/?group_id=45605

to create my forms that I need.

I am not sure where I would use this please? I would suspect that I could call this file as an include just as the user presses "Enter"...but I am usure where to put it please?

I have code snippets of the above to show...if needed, and any help will be greatly appreciated.

Thank's to all, in advnace.
Oct 5 '08 #1
Share this Question
Share on Google+
4 Replies


Markus
Expert 5K+
P: 6,050
The code snippet I posted would be used when a form (using POST) is submitted to a page.
Oct 5 '08 #2

P: 18
Thnak-you for the reply, I have sent a PM to you (sorry)...and sorry for my late reply, but I did not, nor have got a e-mail ( and it is selected) for getting a reply from this topic.

Jim
Oct 5 '08 #3

P: 18
Mello Markus, and all on this site.

I have found a code snippet of what I need here on this site, and not sure where or how to implement it for sure. I am fairly new to pphp/myql myself.

http://bytes.com/forum/thread833623.html

And I used a program called : http://phpformgen.sourceforge.net/ to create my form I need to check for duplicates, prior to db entry.

Below is what the formgen program generates for code, would I just make the file an include? Or put it in here some how?

A main html file and one called process.php (below)

I plan on using the phone number field

Any help greatly appreciated,

Thx...jim


----------
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("global.inc.php");
  3. $errors=0;
  4. $error="The following errors occured while processing your form input.<ul>";
  5. pt_register('POST','Type');
  6. pt_register('POST','Name');
  7. pt_register('POST','Address');
  8. pt_register('POST','City');
  9. pt_register('POST','State');
  10. pt_register('POST','Zip');
  11. pt_register('POST','Phone');
  12. pt_register('POST','EmailExists');
  13. pt_register('POST','EmailAddress');
  14. pt_register('POST','Website');
  15. if($Type=="" || $Name=="" || $Address=="" || $City=="" || $State=="" || $Zip=="" || $Phone=="" || $EmailExists=="" ){
  16. $errors=1;
  17. $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
  18. }
  19. if($errors==1) echo $error;
  20. else{
  21. $where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
  22. $message="Type: ".$Type."
  23. Name: ".$Name."
  24. Address: ".$Address."
  25. City: ".$City."
  26. State: ".$State."
  27. Zip: ".$Zip."
  28. Phone: ".$Phone."
  29. Email Exists: ".$EmailExists."
  30. Email Address: ".$EmailAddress."
  31. Website: ".$Website."
  32. ";
  33. $message = stripslashes($message);
  34. mail("jim@jamesjcotton.com","Form Submitted at your website",$message,"From: phpFormGenerator");
  35. $link = mysql_connect("localhost","db_name","dbpass");
  36. mysql_select_db("emp_skylos",$link);
  37. $query="insert into skylos (Type,Name,Address,City,State,Zip,Phone,Email_Exis ts,Email_Address,Website) values ('".$Type."','".$Name."','".$Address."','".$City."','".$State."','".$Zip."','".$Phone."','".$EmailExists."','".$EmailAddress."','".$Website."')";
  38. mysql_query($query);
  39. $make=fopen("admin/data.dat","a");
  40. $to_put="";
  41. $to_put .= $Type."|".$Name."|".$Address."|".$City."|".$State."|".$Zip."|".$Phone."|".$EmailExists."|".$EmailAddress."|".$Website."
  42. ";
  43. fwrite($make,$to_put);
  44. ?>
  45.  
  46.  
  47. <!-- This is the content of the Thank you page, be careful while changing it -->
  48.  
  49. <h2>Thank you!</h2>
  50.  
  51. <table width=50%>
  52. <tr><td>Type: </td><td> <?php echo $Type; ?> </td></tr>
  53. <tr><td>Name: </td><td> <?php echo $Name; ?> </td></tr>
  54. <tr><td>Address: </td><td> <?php echo $Address; ?> </td></tr>
  55. <tr><td>City: </td><td> <?php echo $City; ?> </td></tr>
  56. <tr><td>State: </td><td> <?php echo $State; ?> </td></tr>
  57. <tr><td>Zip: </td><td> <?php echo $Zip; ?> </td></tr>
  58. <tr><td>Phone: </td><td> <?php echo $Phone; ?> </td></tr>
  59. <tr><td>Email Exists: </td><td> <?php echo $EmailExists; ?> </td></tr>
  60. <tr><td>Email Address: </td><td> <?php echo $EmailAddress; ?> </td></tr>
  61. <tr><td>Website: </td><td> <?php echo $Website; ?> </td></tr>
  62. </table>
  63. <!-- Do not change anything below this line -->
  64.  
  65. <?php 
  66. }
  67. ?>
Oct 5 '08 #4

Markus
Expert 5K+
P: 6,050
Right, I'm back to help!

So, we're trying to catch a duplicate entry? Right.

I explained everything in the other thread, and it looks like I'm going to be explaining it again. Sigh! Doesn't matter, it shall be done.

Using a function called mysql_num_rows() we can find out how many rows in a mysql table matched the query we ran.

Here's the modified version of your code:


Expand|Select|Wrap|Line Numbers
  1. $rows = mysql_query("SELECT * FROM `skylos` WHERE `col1` = '{$data}'");
  2. if( mysql_num_rows( $rows ) > 0 )
  3. {
  4.     echo "Duplicate entry detected";
  5. }
  6. else
  7. {
  8.     $query="
  9.     insert into `skylos` 
  10.         (Type, Name, Address, City, State, Zip, Phone, Email_Exists, Email_Address, Website)
  11.             values 
  12.         (
  13.             '$Type','$Name','$Address','$City','$State',
  14.             '$Zip','$Phone','$EmailExists','$EmailAddress','$Website')
  15.         ";
  16.     mysql_query($query);
  17. }
You'll have to edit the $rows query so that you comparing the right field against the right data.

Cheers,
Markus.
Oct 5 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.