angelali Posted February 25, 2012 Author Share Posted February 25, 2012 I am new in PHP, and I am learning a lot. My objective is not to become an expert in PHP as I am planning to master MySQL and Oracle later. I have successfully made an image hosting, a mini one, with validations of certain types of files etc...but I am adding a registration form in it which actually I am doing. After trying a lot to solve this problem of duplicate in both username and email address, but in vain, I have decided to include the duplicate of email address, not the username...and it is now working... I want a last help from you guys, I am preventing only duplicate email address now, not both the email and the username, and it is working great. Below are my codes, can you please see them and tell me if from this method, I can also prevent the duplicate of username as well? When I say duplicate here, is the message to display the user that the either the email address or username has already taken. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) { //Database $connect = @mysql_connect('localhost', 'root', '') or die ('Connection Failed'); @mysql_select_db('registration', $connect) or die ('Connection Failed'); //Assignng variables $firstname = mysql_real_escape_string(stripslashes(trim($_POST['fname']))); $lastname = mysql_real_escape_string(stripslashes(trim($_POST['lname']))); $email = mysql_real_escape_string(stripslashes(trim($_POST['emailr']))); $uname = mysql_real_escape_string(stripslashes(trim($_POST['user']))); $pwd = mysql_real_escape_string(stripslashes(trim($_POST['pass']))); //Registration codes if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pwd)) { echo '<p class="error">All fields are required to fill!</p>'; return false; } elseif (strlen($firstname) && (strlen($lastname) < '2')) { echo '<p class="error">Invalid first name or last name!</p>'; return false; } elseif (filter_var($firstname, FILTER_VALIDATE_INT) || (filter_var($lastname, FILTER_VALIDATE_INT))) { echo '<p class="error">First name or last name cannot be integers!</p>'; return false; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo '<p class="error">Email address not valid!</p>'; return false; } elseif (strlen($uname) && (strlen($pwd) < '6' )) { echo '<p class="error">Username or password must be minimum 6 characters!</p>'; return false; } else { $pwd = md5($pwd); $query = "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pwd')"; mysql_query($query, $connect); if (mysql_errno($connect)> 0) { echo "Email address already taken"; } else { echo '<p class="fail">Successful!</p>'; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321239 Share on other sites More sharing options...
Pikachu2000 Posted February 25, 2012 Share Posted February 25, 2012 Do you have a UNIQUE index on username, and a UNIQUE index on emailaddress? That should kick out an error if either are duplicates if you do. Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321246 Share on other sites More sharing options...
angelali Posted February 25, 2012 Author Share Posted February 25, 2012 Guys, it is working now... Pikachu, I mentioned that the problem of duplicate has been solved since you told me about the problem of INDEX in the database. I did mention I created another one and now its working. The only problem was the message to display the user that an email or username has already taken was not displaying. By the way, it is working now, YES, the message is displaying! YES...after 4 hours...the problem is solved.. Here the codes I tried, and which are working great: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['fname']) && isset($_POST['lname'])&& isset($_POST['emailr']) && isset($_POST['user']) && isset($_POST['pass'])) { //Database $connect = @mysql_connect('localhost', 'root', '') or die ('Connection Failed'); @mysql_select_db('registration', $connect) or die ('Connection Failed'); //Assignng variables $firstname = mysql_real_escape_string(stripslashes(trim($_POST['fname']))); $lastname = mysql_real_escape_string(stripslashes(trim($_POST['lname']))); $email = mysql_real_escape_string(stripslashes(trim($_POST['emailr']))); $uname = mysql_real_escape_string(stripslashes(trim($_POST['user']))); $pwd = mysql_real_escape_string(stripslashes(trim($_POST['pass']))); //Registration codes if (empty($firstname) || empty($lastname) || empty($email) || empty($uname) || empty($pwd)) { echo '<p class="error">All fields are required to fill!</p>'; return false; } elseif (strlen($firstname) && (strlen($lastname) < '2')) { echo '<p class="error">Invalid first name or last name!</p>'; return false; } elseif (filter_var($firstname, FILTER_VALIDATE_INT) || (filter_var($lastname, FILTER_VALIDATE_INT))) { echo '<p class="error">First name or last name cannot be integers!</p>'; return false; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo '<p class="error">Email address not valid!</p>'; return false; } elseif (strlen($uname) && (strlen($pwd) < '6' )) { echo '<p class="error">Username or password must be minimum 6 characters!</p>'; return false; } else { $pwd = md5($pwd); $query = "INSERT INTO login (id, firstname, lastname, emailaddress, username, password) VALUES('', '$firstname', '$lastname', '$email', '$uname', '$pwd')"; mysql_query($query, $connect); if (mysql_errno($connect)> 0) { echo "The email address or username has already taken, choose another!"; } else { echo '<p class="fail">Successful!</p>'; } } } } ?> I want to thank ALL OF YOU who took so much patience for helping me...ALL OF YOU, THANK YOU VERY MUCH! You have all responded nicely also and as a professional! THANK YOU ALL! Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321248 Share on other sites More sharing options...
Pikachu2000 Posted February 25, 2012 Share Posted February 25, 2012 It may be working, but it really isn't ready to be used on a live web site. You shouldn't be suppressing db connection errors with the @ operator. Those errors should be logged, at the very least. The validation is cumbersome and redundant, yet incomplete. It could also be made more specific for better user-friendliness. stripslashes should not be used without first determining that magic_quotes_gpc is on Values that are to be hashed should left as-is, not escaped or otherwise manipulated filter_var($, FILTER_VALIDATE_INT) on $firstname and $lastname probably isn't doing what you intended it to do I'm not pointing out flaws to have a jab at you; only because you said you wanted to learn. Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321259 Share on other sites More sharing options...
angelali Posted February 25, 2012 Author Share Posted February 25, 2012 So, what you suggest me to correct? Its good you are telling me my mistakes...as I want to correct myself... The FILTER_VALIDATE_INT is doing its job well, I tested it... Huhh, for the striplashes, I have heard that turning Magic_gp ON is not recommended. For the '@', I see that doing that would not display the line if an error occurs. Well, I have learnt all of these in PHPAcademy, you know that guy who always do tutorials on YouTube... Please, suggest me one by one about which to correct... and what I should do.... Thank you for telling my erros..its important to me to learn Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321262 Share on other sites More sharing options...
angelali Posted February 26, 2012 Author Share Posted February 26, 2012 The portfolio is online on a free hosting and sub domain name: mini-image-hosting.99k.org Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321396 Share on other sites More sharing options...
AyKay47 Posted February 26, 2012 Share Posted February 26, 2012 So, what you suggest me to correct? Its good you are telling me my mistakes...as I want to correct myself... The FILTER_VALIDATE_INT is doing its job well, I tested it... Huhh, for the striplashes, I have heard that turning Magic_gp ON is not recommended. For the '@', I see that doing that would not display the line if an error occurs. Well, I have learnt all of these in PHPAcademy, you know that guy who always do tutorials on YouTube... Please, suggest me one by one about which to correct... and what I should do.... Thank you for telling my erros..its important to me to learn Pika has kindly laid out a list of things that should be done differently for you. Take the time to research each of these things and solve them yourself. Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321436 Share on other sites More sharing options...
angelali Posted February 26, 2012 Author Share Posted February 26, 2012 I have already solved the Magic_quotes one, I make an IF statement, so if it is on, then the striplashes take place. The validation also has been adjusted. Quote Link to comment https://forums.phpfreaks.com/topic/257765-prevent-duplicate-content-in-database-does-not-work/page/3/#findComment-1321437 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.