ohguowei Posted September 12, 2007 Share Posted September 12, 2007 Hi i'm a newbie to PhP and i'm doing a webpage login with php5, mysql4 and apache2.2.4. I copy this code from a book and change some of the code a bit. When i try to run it gave me "PHP Notice: Undefined variable: message in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Webpage\LoginPage.Php on line 70" Its the last line of the code "<?php echo $message; ?>" Why is it undefined? i'm confused. Please help <?php if ($_POST[ 'submit' ] == 'Submit') { if (!$_POST['UserName'] || $_POST['UserName'] == "" || strlen($_POST['UserName'] > 30 )){ $message= '<p>There is a problem. Did you enter an user name?</p>'; } else{ //open connection to the database mysql_connect("localhost","root","") or die("Failure to cmmunicate with database!!!"); mysql_select_db("User"); $as_UserName = addslashes($POST['UserName']); $as_UserPassword = addslashes($POST['UserPassword']); $as_UserEmail = addslashes($POST['UserEmail']); $tr_UserName = trim($as_UserName); $tr_UserPassword = trim($as_UserPassword); $tr_UserEmail = trim($as_UserEmail); $query ="INSERT INTO USER_INFORMATION (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (NULL, '$tr_UserName', '$tr_UserPassword', '$tr_UserEmail')"; $result = mysql_query($query); if (mysql_affected_rows()==1){ $message = "<p>Your information has been recorded. Please login and enjoy the game</p>"; $noform_var = 1; } else { error_log(sql_error()); $message = "<p>Something went wrong with your signup attempt. Please email the admin for more information.</p>"; } } //show the form in every case except successful submission if(!$noform_var) { $thisfile = $_SERVER['PHP_SELF']; $message .= <<< EOMSG <FORM METHOD="post" ACTION="$thisfile"> <B>Your Name:<INPUT TYPE="TEXT" SIZE=25 NAME="UserName"> <BR><BR> <B> Your PassWord:<INPUT TYPE="TEXT" SIZE=25 NAME="UserPassword"> <BR><BR> <B> Your Email:<INPUT TYPE="TEXT" SIZE=25 NAME="UserEmail"> <BR><BR> <INPUT TYPE="submit" NAME="submit" VALUE="Submit"> </form> EOMSG; } } ?> <html> <head> <STYLE type ="text/css"> <!-- BODY, p {color:black; font-family: verdana;font-size: 10 pt} H! {color:n;acl; fpmt-family: arial; font-size:12 pt} --> </style> <title>Login Page</title> </head> <body> <table border=0 cellpadding=10 witdth=100%> <tr> <td bgcolor="#F0F8FF" align=center valign=top width=17%> </td> <TD bgcolor="#FFFFFF" align=left valign=top width=83%> <h1> Newsletter sign-up form</h1> <?php echo $message; ?> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/69048-solved-doing-a-login-pagegot-error-undefined-variable/ Share on other sites More sharing options...
watthehell Posted September 12, 2007 Share Posted September 12, 2007 The index message is undefined the first time through ! try this it wont come again <?php $message=""; if ($_POST[ 'submit' ] == 'Submit') { if (!$_POST['UserName'] || $_POST['UserName'] == "" || strlen($_POST['UserName'] > 30 )){ $message= '<p>There is a problem. Did you enter an user name?</p>'; } else{ //open connection to the database mysql_connect("localhost","root","") or die("Failure to cmmunicate with database!!!"); mysql_select_db("User"); $as_UserName = addslashes($POST['UserName']); $as_UserPassword = addslashes($POST['UserPassword']); $as_UserEmail = addslashes($POST['UserEmail']); $tr_UserName = trim($as_UserName); $tr_UserPassword = trim($as_UserPassword); $tr_UserEmail = trim($as_UserEmail); $query ="INSERT INTO USER_INFORMATION (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (NULL, '$tr_UserName', '$tr_UserPassword', '$tr_UserEmail')"; $result = mysql_query($query); if (mysql_affected_rows()==1){ $message = "<p>Your information has been recorded. Please login and enjoy the game</p>"; $noform_var = 1; } else { error_log(sql_error()); $message = "<p>Something went wrong with your signup attempt. Please email the admin for more information.</p>"; } } //show the form in every case except successful submission if(!$noform_var) { $thisfile = $_SERVER['PHP_SELF']; $message .= <<< EOMSG <FORM METHOD="post" ACTION="$thisfile"> <B>Your Name:<INPUT TYPE="TEXT" SIZE=25 NAME="UserName"> <BR><BR> <B> Your PassWord:<INPUT TYPE="TEXT" SIZE=25 NAME="UserPassword"> <BR><BR> <B> Your Email:<INPUT TYPE="TEXT" SIZE=25 NAME="UserEmail"> <BR><BR> <INPUT TYPE="submit" NAME="submit" VALUE="Submit"> </form> EOMSG; } } ?> <html> <head> <STYLE type ="text/css"> <!-- BODY, p {color:black; font-family: verdana;font-size: 10 pt} H! {color:n;acl; fpmt-family: arial; font-size:12 pt} --> </style> <title>Login Page</title> </head> <body> <table border=0 cellpadding=10 witdth=100%> <tr> <td bgcolor="#F0F8FF" align=center valign=top width=17%> </td> <TD bgcolor="#FFFFFF" align=left valign=top width=83%> <h1> Newsletter sign-up form</h1> <?php echo $message; ?> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/69048-solved-doing-a-login-pagegot-error-undefined-variable/#findComment-347082 Share on other sites More sharing options...
ohguowei Posted September 12, 2007 Author Share Posted September 12, 2007 i try running the code with $message defined on the top of the script but i didnt get the FORM that i wanted. Why should i define the $message on the top of the script? and why doesnt the FORM INPUT display on the page? Is the logic of my coding wrong in the first place? Some more help please? if(!$noform_var) { $thisfile = $_SERVER['PHP_SELF']; $message .= <<< EOMSG <FORM METHOD="post" ACTION="$thisfile"> <B>Your Name:<INPUT TYPE="TEXT" SIZE=25 NAME="UserName"> <BR><BR> <B> Your PassWord:<INPUT TYPE="TEXT" SIZE=25 NAME="UserPassword"> <BR><BR> <B> Your Email:<INPUT TYPE="TEXT" SIZE=25 NAME="UserEmail"> <BR><BR> <INPUT TYPE="submit" NAME="submit" VALUE="Submit"> </form> EOMSG; } Quote Link to comment https://forums.phpfreaks.com/topic/69048-solved-doing-a-login-pagegot-error-undefined-variable/#findComment-347095 Share on other sites More sharing options...
watthehell Posted September 12, 2007 Share Posted September 12, 2007 check this hope it works... <?php $message=""; if ($_POST['submit']=='Submit') { if (!$_POST['UserName'] || $_POST['UserName'] == "" || strlen($_POST['UserName'] > 30 )){ $message= '<p>There is a problem. Did you enter an user name?</p>'; } else{ //open connection to the database mysql_connect("localhost","root","") or die("Failure to cmmunicate with database!!!"); mysql_select_db("User"); $as_UserName = addslashes($POST['UserName']); $as_UserPassword = addslashes($POST['UserPassword']); $as_UserEmail = addslashes($POST['UserEmail']); $tr_UserName = trim($as_UserName); $tr_UserPassword = trim($as_UserPassword); $tr_UserEmail = trim($as_UserEmail); $query ="INSERT INTO USER_INFORMATION (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (NULL, '$tr_UserName', '$tr_UserPassword', '$tr_UserEmail')"; $result = mysql_query($query); if (mysql_affected_rows()==1){ $message = "<p>Your information has been recorded. Please login and enjoy the game</p>"; $noform_var=1; } else { error_log(sql_error()); $message = "<p>Something went wrong with your signup attempt. Please email the admin for more information.</p>"; } } //show the form in every case except successful submission if($noform_var!=1) { $thisfile = $_SERVER['PHP_SELF']; $message .= <<< EOMSG; echo "<FORM METHOD=\"post\"> <B>Your Name:<INPUT TYPE=\"TEXT\" SIZE=25 NAME=\"UserName\"> <BR><BR> <B> Your PassWord:<INPUT TYPE=\"TEXT\" SIZE=25 NAME=\"UserPassword\"> <BR><BR> <B> Your Email:<INPUT TYPE=\"TEXT\" SIZE=25 NAME=\"UserEmail\"> <BR><BR> <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE="Submit"> </form>"; } } ?> <html> <head> <STYLE type ="text/css"> <!-- BODY, p {color:black; font-family: verdana;font-size: 10 pt} H! {color:n;acl; fpmt-family: arial; font-size:12 pt} --> </style> <title>Login Page</title> </head> <body> <table border=0 cellpadding=10 witdth=100%> <tr> <td bgcolor="#F0F8FF" align=center valign=top width=17%> </td> <TD bgcolor="#FFFFFF" align=left valign=top width=83%> <h1> Newsletter sign-up form</h1> <?php echo $message; ?> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/69048-solved-doing-a-login-pagegot-error-undefined-variable/#findComment-347114 Share on other sites More sharing options...
ohguowei Posted September 12, 2007 Author Share Posted September 12, 2007 hey i got it to work! its the logic itself that is having problem. i keep asking the code to run everything in one IF, that why it doesnt work ! thanks for the help even if its not the answer i'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/69048-solved-doing-a-login-pagegot-error-undefined-variable/#findComment-347160 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.