Computing.Net > Forums > Web Development > undefined variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

undefined variable

Reply to Message Icon

Name: BottyZ
Date: October 21, 2009 at 03:49:50 Pacific
OS: Windows XP
Product: N/a / N/A
Subcategory: PHP
Tags: php, variable, MySQL, undefined
Comment:

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>



Sponsored Link
Ads by Google

Response Number 1
Name: Fist (by fmwap)
Date: October 21, 2009 at 17:29:56 Pacific
Reply:

It's not an error, it's a notice.

error_reporting(E_ERROR);

should do it


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: undefined variable

$PATH php error www.computing.net/answers/webdevel/path-php-error/294.html

PHP - Help pls! www.computing.net/answers/webdevel/php-help-pls/1987.html

php error www.computing.net/answers/webdevel/php-error/1208.html