Jump to content

[SOLVED] Doing a login page,got error Undefined variable


ohguowei

Recommended Posts

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>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.