undefined variable

October 21, 2009 at 03:49:50
Specs: Windows XP
Basically, i'm creating a new website and want a php mysql news page... meaning the content is loaded from the mysql and php writes the page based on whats in there. The problem i'm having is with password protecting my 'add news' page. Obviously i don't want anybody to add news articles so the functions for this are hidden until the correct pass is entered.

But everytime i load the php page i receive the following error:

Notice: Undefined index: pass in C:\wamp\www\add.php on line 11

But if i click submit without entering anything the error disappears and the page then works and looks how it should. How do i stop the page bringing up the initial error?


the code is as follows:

<html>
<head>
<title>Add News</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>
<body>
<?php
define('PASSWORD','bacon'); // YOUR PASSWORD HERE
session_start();
if(!isset($_SESSION['pass'])){
$pass = $_REQUEST['pass'];
}
if($pass == PASSWORD){
$_SESSION['pass'] = "true";
} else {
unset($pass);
unset($_SESSION['pass']);
}
if(isset($_SESSION['pass'])){
echo "You Passed!";
if(isset($add_n)){
   $link = @mysql_connect(localhost, username, password);
   if(!$link){
      echo('Error connecting to the database: ' . $mysql_error());
      exit();
   }
   $db = @mysql_selectdb('mydatabase');
   if(!$db){
      echo('Error selecting database: ' . $mysql_error());
      exit();
   }
   $query = "INSERT INTO news(name, email, headline, story, timestamp)VALUES('$name', '$email', '$headline', '$story', NOW())";
   $result = @mysql_query($query);
   if(!$result){
      echo('Error adding news: ' . $mysql_error());
      exit();
   }else{
   mysql_close($link);
   echo('Success!
<a href="add.php">Click here</a> to add more news.
<a href="edit.php">Click here</a> to edit news.
<a href="../index.php">Click here</a> to return to the main page.');
   }
}else{

echo('<form name="form1" method="post" action="$PHP_SELF">');
echo('<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr> 
      <td width="50%">Name</td>
      <td><input name="name" type="text" id="name"></td>
    </tr>
    <tr> 
      <td>Email</td>
      <td><input name="email" type="text" id="email"></td>
    </tr>
    <tr> 
      <td> </td>
      <td> </td>
    </tr>
    <tr> 
      <td>Headline</td>
      <td><input name="headline" type="text" id="headline"></td>
    </tr>
    <tr> 
      <td>News Story</td>
      <td><textarea name="story" id="story"></textarea></td>
    </tr>
    <tr> 
      <td colspan="2"><div align="center">
          <input name="hiddenField" type="hidden" value="add_n">
          <input name="add" type="submit" id="add" value="Submit">
        </div></td>
    </tr>
  </table>
  </form>');
} 

// PROTECTED CONTENT HERE
} else {
// UNPROTECTED CONTENT
echo "<form name=\"login_form\" 
method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
<b>Password:</b> <input type=\"password\" name=\"pass\"> 
<input type=\"submit\" name=\"Submit\" value=\"Submit\">
</form>";
}
?>


Today's Date is
<?php
  echo( date("l, F dS Y.") );
?>

</body>
</html>


See More: undefined variable

Report •


#1
October 21, 2009 at 17:29:56
It's not an error, it's a notice.

error_reporting(E_ERROR);

should do it


Report •
Related Solutions


Ask Question