jreed2236 Posted October 22, 2009 Share Posted October 22, 2009 I'm making a register/login form and I keep getting this error: here is my code: <?php echo "<h1>Register</h1>"; //form data $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; if ($submit) { //open database $connect = mysql_connect("-","-","-"); mysql_select_db("-"); //select database $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'"); $count = mysql_num_rows($namecheck); if ($count!=0) { die("Username Taken."); } //check for existance if ($fullname&&$username&&$password&&$repeatpassword) { if ($password==$repeatpassword) { //check char length of username and fullname if (strlen($username)>25||strlen($fullname)>25) { echo "Length of username is too long!"; } else { //check password length if (strlen($password)>25||strlen(password)<6) { echo "Password must be between 6 and 25 characters."; } else { //register user! //encrypt password $password = md5($password); $repeatpassword = md5($repeatpassword); //generate random number for activation proccess $random = rand(23456789,98765432); $queryreg = mysql_query(" INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0') "); die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>"); } } } else echo "Your passwords do not match."; } else echo "Please fill in <b>all</b> fields"; } ?> <html> <p> <form action='http://www.zomvie.com/register.php' method='POST'> <table> <tr> <td> Your full name: </td> <td> <input type='text' name='fullname' value='<?php echo $fullname; ?>'> </td> </tr> <tr> <td> Choose a username: </td> <td> <input type='text' name='username'value='<?php echo $username; ?>'> </td> </tr> <tr> <td> Choose a password: </td> <td> <input type='password' name='password'> </td> </tr> <tr> <td> repeat your password: </td> <td> <input type='password' name='repeatpassword'> </td> </tr> <tr> <td> Email Address: </td> <td> <input type='text' name='email'> </td> </tr> </table> <p> <input type='submit' name='submit' value='Register'> </html> Can someone tell me whats wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/ Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 that happens because you try to set variables to POST data that doesn't exist yet. you should do something like if (isset($_POST['submit'])){ $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; //etc. What happens is you try to access the $_POST array with indexs that don't exist yet. Which is why the error says undefined index Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942267 Share on other sites More sharing options...
jreed2236 Posted October 22, 2009 Author Share Posted October 22, 2009 I posted what you suggested and it returned the error Parse error: syntax error, unexpected $end in /www/zxq.net/j/r/e/jreed2236/htdocs/register.php on line 138 Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942275 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 thats probably because you took what I posted and just slapped it in the middle of your code, instead of changing your code to model what I posted. If i were to guess (and this is just a guess because you haven't posted your updated code) assuming you just copy pasted what I typed, you are missing the closing curly brace ('}') to end the if statement Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942280 Share on other sites More sharing options...
jreed2236 Posted October 22, 2009 Author Share Posted October 22, 2009 Here is what I did: //form data if (isset($_POST['submit'])) { $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; } Do I have to do that for every one of the variables? (the 'isset' part) Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942283 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 what you did there is fine. Unexpected $end errors pop up usually when you are missing an ending curly bracket somewhere in your code. Try posting the whole script so I can take a look Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942286 Share on other sites More sharing options...
jreed2236 Posted October 22, 2009 Author Share Posted October 22, 2009 Alright here is the updated code: <?php echo "<h1>Register</h1>"; //form data if (isset($_POST['submit'])) { $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; if ($submit) { //open database $connect = mysql_connect("-","-","-"); mysql_select_db("-"); //select database $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'"); $count = mysql_num_rows($namecheck); if ($count!=0) { die("Username Taken."); } //check for existance if ($fullname&&$username&&$password&&$repeatpassword) { if ($password==$repeatpassword) { //check char length of username and fullname if (strlen($username)>25||strlen($fullname)>25) { echo "Length of username is too long!"; } else { //check password length if (strlen($password)>25||strlen(password)<6) { echo "Password must be between 6 and 25 characters."; } else { //register user! //encrypt password $password = md5($password); $repeatpassword = md5($repeatpassword); //generate random number for activation proccess $random = rand(23456789,98765432); $queryreg = mysql_query(" INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0') "); die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>"); } } } else echo "Your passwords do not match."; } else echo "Please fill in <b>all</b> fields"; } ?> <html> <p> <form action='http://www.zomvie.com/register.php' method='POST'> <table> <tr> <td> Your full name: </td> <td> <input type='text' name='fullname' value='<?php echo $fullname; ?>'> </td> </tr> <tr> <td> Choose a username: </td> <td> <input type='text' name='username'value='<?php echo $username; ?>'> </td> </tr> <tr> <td> Choose a password: </td> <td> <input type='password' name='password'> </td> </tr> <tr> <td> repeat your password: </td> <td> <input type='password' name='repeatpassword'> </td> </tr> <tr> <td> Email Address: </td> <td> <input type='text' name='email'> </td> </tr> </table> <p> <input type='submit' name='submit' value='Register'> </html> now when i refresh the page most of it is gone but I get this: Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942290 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 ok.. yeah as I thougth you just copy pasted what I posted at the top of your code. this line if (isset($_POST['submit'])) was meant to replace this line if ($submit) since essentially they do the same thing, and this stuff, $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; should go after the isset test: if (isset($_POST['submit'])) but if you want to fix your code the easy way, you seem to be missing a closing curly bracket at the end of your nested if/elses. just add one after this bit } else echo "Please fill in <b>all</b> fields"; } either way it will do the same thing so it doesn't matter much Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942294 Share on other sites More sharing options...
jreed2236 Posted October 22, 2009 Author Share Posted October 22, 2009 Alright just one last thing. on the inside of my Full Name box it says: <br /> <b>Notice</b>: Undefined variable: fullname in <b>/www/zxq.net/j/r/e/jreed2236/htdocs/register.php</b> on line <b>97</b><br /> and on the inside of my choose a username box it says: <br /> <b>Notice</b>: Undefined variable: username in <b>/www/zxq.net/j/r/e/jreed2236/htdocs/register.php</b> on line <b>106</b><br /> other than that everything is fine..any suggestions? <?php echo "<h1>Register</h1>"; //form data if (isset($_POST['submit'])) { $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; { //open database $connect = mysql_connect("-","-","-"); mysql_select_db("-"); //select database $namecheck = mysql_query("SELECT username FROM users WHERE username='$username'"); $count = mysql_num_rows($namecheck); if ($count!=0) { die("Username Taken."); } //check for existance if ($fullname&&$username&&$password&&$repeatpassword) { if ($password==$repeatpassword) { //check char length of username and fullname if (strlen($username)>25||strlen($fullname)>25) { echo "Length of username is too long!"; } else { //check password length if (strlen($password)>25||strlen(password)<6) { echo "Password must be between 6 and 25 characters."; } else { //register user! //encrypt password $password = md5($password); $repeatpassword = md5($repeatpassword); //generate random number for activation proccess $random = rand(23456789,98765432); $queryreg = mysql_query(" INSERT INTO users VALUES ('','$fullname','$username','$password','$email','$date','$random','0') "); die("You have been registered! <a href='http://www.zomvie.com/index.php'>Return to login page</a>"); } } } else echo "Your passwords do not match."; } else echo "Please fill in <b>all</b> fields"; } } ?> <html> <p> <form action='http://www.zomvie.com/register.php' method='POST'> <table> <tr> <td> Your full name: </td> <td> <input type='text' name='fullname' value='<?php echo $fullname; ?>'> </td> </tr> <tr> <td> Choose a username: </td> <td> <input type='text' name='username'value='<?php echo $username; ?>'> </td> </tr> <tr> <td> Choose a password: </td> <td> <input type='password' name='password'> </td> </tr> <tr> <td> repeat your password: </td> <td> <input type='password' name='repeatpassword'> </td> </tr> <tr> <td> Email Address: </td> <td> <input type='text' name='email'> </td> </tr> </table> <p> <input type='submit' name='submit' value='Register'> </html> Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942300 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 that happens because you don't define either of those variables unless you submit the form, because of the following: if (isset($_POST['submit'])) why would you want to populate the form with something that would be empty anyways? just remove the <?php echo $username; ?> and <?php echo $fullname; ?> stuff. but one thing i noticed if (isset($_POST['submit'])) { $submit = $_POST['submit']; $fullname = strip_tags($_POST['fullname']); $username = strtolower(strip_tags($_POST['username'])); $password = strip_tags($_POST['password']); $repeatpassword = strip_tags($_POST['repeatpassword']); $date = ("date.America/New_York"); $email = $_POST['email']; {//this??? that opening curly brace doesn't seem to need to be there. Honestly, I'm surprised that you don't get a syntax error Quote Link to comment https://forums.phpfreaks.com/topic/178642-solved-notice-undefined-index/#findComment-942304 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.