ecabrera Posted August 17, 2011 Share Posted August 17, 2011 i create this registration script where users register and after registering the will get a email sayin 'you need to activate your account ' but the problem is that when i register a account the information does not appear in the in my mysql database and it also does not send out emails and i tryed ini_set( 'display_errors', 1 ); error_reporting( -1 ); and all i get is Notice: Undefined index: submitbtn in /home/ecabrera/public_html/register.php on line 56 which is if ($_POST['submitbtn']){ this is the whole script <?php $title = "Register"; ?> <?php require("styles/top.php"); ?> <div id='full'> <?php $form = "<form action='register.php' method='post' enctype='multipart/form-data'> <table cellspacing='10px'> <tr> <td></td> <td><font color='red'>*</font> are required</td> </tr> <tr> <td>First Name:</td> <td><input type='text' name='firstname' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Last Name:</td> <td><input type='text' name='lastname' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Username:</td> <td><input type='text' name='username' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Email:</td> <td><input type='text' name='email' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='password' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Confirm Password:</td> <td><input type='password' name='repassword' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Avatar:</td> <td><input type='file' name='avatar'></td> </tr> <tr> <td>Youtube Username:</td> <td><input type='text' name='youtube' class='textbox' size='35'></td> </tr> <tr> <td>Bio/About:</td> <td><textarea name='bio' cols='35' rows='5' class='textbox'></textarea></td> </tr> <tr> <td></td> <td><input type='submit' name='submitbtn' value='Register' class='button'></td> </tr> </table> </form>"; if ($_POST['submitbtn']){ $firstname = fixtext($_POST['firstname']); $lastname = fixtext($_POST['lastname']); $username = fixtext($_POST['username']); $email = fixtext($_POST['email']); $password = fixtext($_POST['password']); $repassword = fixtext($_POST['repassword']); $youtube = fixtext($_POST['youtube']); $bio = fixtext($_POST['bio']); $name = $_FILES['avatar']['name']; $type = $_FILES['avatar']['type']; $size = $_FILES['avatar']['size']; $tmpname = $_FILES['avatar']['tmp_name']; $ext = substr($name, strrpos($name, '.')); if ($firstname && $lastname && $username && $email && $password && $repassword){ if ($password == $repassword){ if (strstr($email, "@") && strstr($email, ".") && (strlen($email) >= 6)){ require("scripts/connect.php"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $numrows = mysql_num_rows($query); if ($numrows == 0){ $query = mysql_query("SELECT * FROM users WHERE email='$email'"); $numrows = mysql_num_rows($query); if ($numrows == 0){ $pass = md5(md5($password)); $date = date("F d, Y"); if ($name){ $avatarname = $username.$ext; move_uploaded_file($tmpname, "avatars/$avatarname"); $avatar = $username.$ext; } else $avatar = "defavatar.png"; //will be random number for activating account $code = rand(23456789,98765432); //the frist 0 =active and secand 0 =locked mysql_query("INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '$youtube', '', '0', '$code', '0', '$date')"); //gets the next id in the users table $lastid = mysql_insert_id(); //sends email $to = $email; $subject = "Activate your Account"; $headers = "From: admin@mywebsite.com"; $body ="Hello $firstname,\n\n You need to activate your account with the link below: http://www.mywebsite.com/activate.php?id=$lastid&code=$code \n\n Thanks "; //function to send email mail ($to, $subject, $body, $headers); die("You have been register!<a href='http://www.website.com/index.php'>Log In</a>"); } else echo "That email is already taken. $form"; } else echo "That username is already taken. $form"; } else echo "You did not enter a valid email. $form"; } else echo "Your passwords did not match. $form"; } else echo "You did not fill in all the required fields. $form"; } else echo "$form"; ?> </div> <?php require("styles/bottom.php"); ?> Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 17, 2011 Author Share Posted August 17, 2011 well i got the email activation to work i used this code works fine and its faster but the info does not get transfer to mysql //sends email $webmaster = "Admin@mywesbite.com"; $subject = "Activate Your Account"; $headers = "From: mywebsite<$webmaster>"; $message = "Hello $firstname. Welcome to mywesbite.com Below is a link for you to activate your account on mywesbite.com\n\n Click Here to Activate Your Account: http://wwwwebsite.com/activate.php?id=$lastid&code=$code"; mail($email, $subject, $message, $headers); echo "Thank You for registering. You must now activate your account throught the activation email that has been sent."; Quote Link to comment Share on other sites More sharing options...
Genesis730 Posted August 17, 2011 Share Posted August 17, 2011 Change line 56 from if ($_POST['submitbtn']) { to if(isset($_POST['submitbtn'])) { I believe you need to call isset when checking to see if a form is submitted or not Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 So I'm assuming in the email, $lastid is blank? You aren't checking for MySQL errors on this line mysql_query("INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '$youtube', '', '0', '$code', '0', '$date')"); Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 17, 2011 Author Share Posted August 17, 2011 i use this //gets the next id in the users table $lastid = mysql_insert_id(); im trying to get the next id in my mysql db Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 You didn't read what I typed. The issue is your INSERT query is failing, and you're not checking for errors. Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 17, 2011 Author Share Posted August 17, 2011 sorry i dont understand you Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Then sorry, I can't help you. Is this a language barrier? Or are you unsure how to check for MySQL errors? Check out mysql_error() in the PHP manual. http://www.php.net/manual/en/function.mysql-error.php Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 17, 2011 Author Share Posted August 17, 2011 im unsure about mysql errors Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Read that page in the manual. It's very easy to use, and they provide working examples. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 17, 2011 Share Posted August 17, 2011 There are lots of problems with your code. I have gone through and completely reworked the structure, but not resolved all the problems. Here are some issues: 1. You have a die() clause for the main success scenario! Redirect the user to a confirmation page that their account was created 2. The verification error check and the actual error messages are widely separated. Don't make it hard on yourself put the error messages where the error check is. 3. FONT tags have been deprecated for YEARS! Stop using them and use style properties. 4. No validations for processing errors (query errors or send mail errors) I also changed the form so it will repopulate with the submitted data if there are validation errors> there is a lot more I would change, but don't have the time. But, the structure is much more logical and easier to read. I haven't tested it (since I don't have your DB), so there might be some typos/syntax errors <?php $title = "Register"; $errors = array(); $errorMsg = ''; if (isset($_POST['submitbtn'])) { $firstname = fixtext($_POST['firstname']); $lastname = fixtext($_POST['lastname']); $username = fixtext($_POST['username']); $email = fixtext($_POST['email']); $password = fixtext($_POST['password']); $repassword = fixtext($_POST['repassword']); $youtube = fixtext($_POST['youtube']); $bio = fixtext($_POST['bio']); $name = $_FILES['avatar']['name']; $type = $_FILES['avatar']['type']; $size = $_FILES['avatar']['size']; $tmpname = $_FILES['avatar']['tmp_name']; $ext = substr($name, strrpos($name, '.')); //Validate input if ($firstname && $lastname && $username && $email && $password && $repassword) { $errors[] = "You did not fill in all the required fields."; } elseif ($password == $repassword) { $errors[] = "Your passwords did not match."; } elseif (strstr($email, "@") && strstr($email, ".") && (strlen($email) >= 6)) { $errors[] = "You did not enter a valid email."; } else { //Initial input has been validated, check for duplicates require("scripts/connect.php"); $query = "SELECT * FROM users WHERE username='$username'"; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($resutl) != 0) { $errors[] = "That username is already taken."; } $query = "SELECT * FROM users WHERE email='$email'"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($resutl) != 0) { $errors[] = "That email is already taken."; } } //If there were errors define error message(s) if(count($errors)>0) { //Define the error message $errorMsg = "The following errors occured:<ul>\n"; foreach($errors as $errorTxt) { $errorMsg .= "<li>{$errorTxt}</li>\n"; } $errorMsg .= "</ul>\n"; } else { //There were no errors, process the data $pass = md5(md5($password)); $date = date("F d, Y") $avatar = "defavatar.png"; if ($name) { $avatar = $username.$ext; move_uploaded_file($tmpname, "avatars/$avatar"); } $code = rand(23456789,98765432); $query = "INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '$youtube', '', '0', '$code', '0', '$date')"; $result = mysql_query($query) or die(mysql_error()); //gets the next id in the users table $lastid = mysql_insert_id(); $to = $email; $subject = "Activate your Account"; $headers = "From: admin@mywebsite.com"; $body ="Hello $firstname,\n\n You need to activate your account with the link below: http://www.mywebsite.com/activate.php?id=$lastid&code=$code \n\n Thanks"; //function to send email if(mail ($to, $subject, $body, $headers)) { //Email was sent header("successpage.php"); } else { $errorMsg = "Unable to send registration email."; } } } ?> <?php require("styles/top.php"); ?> <div id='full'> <span style="color:red;"><?php echo $errorMsg; ?></span> <br><br> <form action='register.php' method='post' enctype='multipart/form-data'> <table cellspacing='10px'> <tr> <td></td> <td><font color='red'>*</font> are required</td> </tr> <tr> <td>First Name:</td> <td><input type='text' name='firstname' class='textbox' size='35' value='<?php echo htmlentities($_POST['firstname']); ?>'><font color='red'>*</font></td> </tr> <tr> <td>Last Name:</td> <td><input type='text' name='lastname' class='textbox' size='35' value='<?php echo htmlentities($_POST['lastname']); ?>'><font color='red'>*</font></td> </tr> <tr> <td>Username:</td> <td><input type='text' name='username' class='textbox' size='35' value='<?php echo htmlentities($_POST['username']); ?>'><font color='red'>*</font></td> </tr> <tr> <td>Email:</td> <td><input type='text' name='email' class='textbox' size='35' value='<?php echo htmlentities($_POST['email']); ?>'><font color='red'>*</font></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='password' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Confirm Password:</td> <td><input type='password' name='repassword' class='textbox' size='35'><font color='red'>*</font></td> </tr> <tr> <td>Avatar:</td> <td><input type='file' name='avatar'></td> </tr> <tr> <td>Youtube Username:</td> <td><input type='text' name='youtube' class='textbox' size='35' value='<?php echo htmlentities($_POST['youtube']); ?>'></td> </tr> <tr> <td>Bio/About:</td> <td><textarea name='bio' cols='35' rows='5' class='textbox' value='<?php echo htmlentities($_POST['bio']); ?>'></textarea></td> </tr> <tr> <td></td> <td><input type='submit' name='submitbtn' value='Register' class='button'></td> </tr> </table> </form> </div> <?php require("styles/bottom.php"); ?> Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 This will cause it to fail: elseif ($password == $repassword) Should be: elseif ($password != $repassword) I would suggest using what MJ gave you, it is high quality! Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 17, 2011 Author Share Posted August 17, 2011 ok i still donet get why i cant create a user in my db Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 18, 2011 Share Posted August 18, 2011 If you cannot create a user in the script provided, that script should tell you why. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2011 Share Posted August 18, 2011 ok i still donet get why i cant create a user in my db As was already stated previously you have no error handling on your query. Therefore, if the query fails there is no report that there was an error and the script continues on as if there were no problems. That is why you need to add error handling to your db queries. Displaying the raw output of mysql_error() to a user is not advised. But, for now, with developing your script go ahead and check if there was an error. If so, display the error to the page (and the query) to the page. You can work on implementing a more elegant error reporting system later. But, for now, I would say there is a 99% chance your query is failing. The error message generated would tell you what the problem is or at least point you in the right direction. But, since you are not displaying it or telling us what the error is we have no way of knowing what the error is. It could be something as simple as a typo of a field name or something more complex. Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 18, 2011 Author Share Posted August 18, 2011 It gives me a server error Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 18, 2011 Share Posted August 18, 2011 The easiest way for us to understand the error, is to copy/paste it. Quote Link to comment Share on other sites More sharing options...
ecabrera Posted August 18, 2011 Author Share Posted August 18, 2011 its not a php error its the script when i add the script that mj gave me and when i refresh it gives me a server error Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 18, 2011 Share Posted August 18, 2011 $date = date("F d, Y") Should be: $date = date("F d, Y"); Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2011 Share Posted August 18, 2011 its not a php error its the script when i add the script that mj gave me and when i refresh it gives me a server error OK, I did not test that code - how could I since I don't have your database - so there may be some syntax/typos. I expect the person receiving the code I provide to be able to debug those types of errors. Assuming there were no syntax errors, or you fixed them, that error may be from MySQL. That is what has been stated several times now - that your query is failing. You have been asked several times to provide that error. If you want help, please provide the error. Even if it isn't the MySQL error we would probably be able to provide a solution to that error. Quote Link to comment 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.