angelali Posted February 21, 2012 Share Posted February 21, 2012 I have created an input field on a website for people to subscribe by their email address. The email address is stored in a database. I am using PHPMyAdmin. The email address is successfully working, but I want to prevent duplicate email address to be stored, however, I am having an error. Here are my codes: HTML codes: <form action="index.php" method="post"> <input type="text" size="25" placeholder="Your email address..." name="enter"/> <input class="submit" type="submit" value="Subscribe" name="subscribe"/> <br/> PHP with Query codes: <?php if ( $_SERVER['REQUEST_METHOD'] == "POST" ) { $ee = htmlentities($_POST['enter']); if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){ echo '<p class="fail">Failed...Try again!</p>'; } else { @mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!'); @mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!'); $duplicate = "SELECT * FROM `email` WHERE `emailaadress` = '{$ee}'"; $query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')"; $result = mysql_query($duplicate); if ( mysql_num_rows ( $result ) > 1) { /* Username already exists */ echo 'Username already exists'; } else { mysql_query($query); echo '<p class="success">Successfully subscribed!</p>'; } } } ?> Error I am having: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\softwareportfolio\index.php on line 68 Can someone help me? Thank Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/ Share on other sites More sharing options...
ManiacDan Posted February 21, 2012 Share Posted February 21, 2012 You're not doing any error-handling on these queries. Use mysql_error to view an error if the result of mysql_query is false. Also, you spelled address wrong in $duplicate, that's the actual problem. Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319543 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 And the main problem is, how to do it! I am new in PHP and after searching tremendously, I have found this code! Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319547 Share on other sites More sharing options...
ManiacDan Posted February 21, 2012 Share Posted February 21, 2012 In your code here, $result is the result of mysql_query. $result is either: A) A mysql_resultset resource, on which you can call mysql_fetch_array, mysql_num_rows, etc. B) false, which indicates a query error. So: if ( $result == false ) { echo "THERE WAS AN ERROR: " . mysql_error(); die(); } Of course, there are far better ways to handle errors, but this will actually show you on the screen what the error is. You should also echo the query itself, so it shows up alongside the error and you can see if something is misspelled, like it is in this case. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319551 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 It gives me this: Unknown column 'emailaadress' in 'where clause' I forgot to mention that the emailaddress field is UNIQUE key Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319556 Share on other sites More sharing options...
ManiacDan Posted February 21, 2012 Share Posted February 21, 2012 Also, you spelled address wrong in $duplicate, that's the actual problem.Remember that? Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319578 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 Yes, I corrected the error of $duplicate. Again the problem still here.. Here the codes you stated to me to correct: <?php if ( $_SERVER['REQUEST_METHOD'] == "POST" ) { $ee = htmlentities($_POST['enter']); if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){ echo '<p class="fail">Failed...Try again!</p>'; } else { @mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!'); @mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!'); $duplicate = "SELECT * FROM `email` WHERE `emailaddress` = '.$ee.'"; $query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '.$ee')"; $result = mysql_query($duplicate); if ( $result == false ) { echo "THERE WAS AN ERROR: " . mysql_error(); die(); } else { mysql_query($query); echo '<p class="success">Successfully subscribed!</p>'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319590 Share on other sites More sharing options...
ManiacDan Posted February 21, 2012 Share Posted February 21, 2012 So you get the error about "emailaadress" without "emailaadress" being in the code anymore? Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319595 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 After correcting about the $duplicate which you told me, I am not getting any error now! The only problem is I am not getting the message which says that the email address is already registered! It is still saving the email address. Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319597 Share on other sites More sharing options...
ManiacDan Posted February 21, 2012 Share Posted February 21, 2012 There is no such output in your code. Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319604 Share on other sites More sharing options...
litebearer Posted February 21, 2012 Share Posted February 21, 2012 Perhaps... <?php if ( $_SERVER['REQUEST_METHOD'] == "POST" ) { $ee = htmlentities($_POST['enter']); if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$ee) || empty($ee)){ echo '<p class="fail">Failed...Try again!</p>'; die(); /* send them back to form */ } mysql_connect ('localhost', 'root', '') or die ('A problem has occurred, refresh the page and try again!'); mysql_select_db ('links') or die ('A problem has occurred, refresh the page and try again!'); $duplicate = "SELECT * FROM `email` WHERE `emailaddress` = '$ee'"; $query = "INSERT INTO email (id, emailaddress) VALUES('NULL', '$ee')"; $result = mysql_query($duplicate); if(mysql_num_rows>0) { echo "That address already exists"; die(); /* send them back to form */ } $result2 = mysql_query($query); if ( $result2 == false ) { echo "THERE WAS AN ERROR: " . mysql_error(); die(); /* send them back to form */ } echo '<p class="success">Successfully subscribed!</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319608 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 Litebearer, Nope, I tried it but in vain! I have tried many many methods, but in vain. I have noticed that several posts on this are on the net...but no good answers yet... I am asking myself if should I let it like this! Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319612 Share on other sites More sharing options...
angelali Posted February 21, 2012 Author Share Posted February 21, 2012 I got this Litebearer: Notice: Use of undefined constant mysql_num_rows - assumed 'mysql_num_rows' in C:\xampp\htdocs\softwareportfolio\index.php on line 59 THERE WAS AN ERROR: Duplicate entry 'alithebest@gmail.com' for key 'emailaddress' Still trying to figure out... Quote Link to comment https://forums.phpfreaks.com/topic/257464-duplicate-content-in-database/#findComment-1319613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.