How to remove E_ALL errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

How to remove E_ALL errors

Post by shivam0101 »

This is the code i wrote. It works fine without E_ALL error reporting. I read in a php tutorial that it is a good practice to turn error_reporting to E_ALL. If it is turned on, i get Undefined variable: for all the variables. How to debug this? I tried to used isset, it's not showing error for process, member_name, member_passord. Now i am getting
Notice: Undefined variable: member_name in C:\wamp\www\message\index.php on line 36
Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 40

should i have to use isset() for all variables? Is there any other method to solve this?


Code: Select all

<?php
   error_reporting(E_ALL);

   require_once('config/includes.php');
   
   if(isset($_POST['process']))                    $process=$_POST['process'];
   if(isset($_POST['member_name']))           $member_name=$_POST['member_name'];
   if(isset($_POST['member_password']))       $member_password=$_POST['member_password'];
   
   if(isset($process))
   {      
      switch($process)
      {
         case 'submit':
         submit();
          
         case 'show_form':
         show_form(); 
   
         default: show_form();
      }
    }
    else
    {
       show_form();
    }
     
   
   function show_form()
   {
     $login_table=
     "<form method='post' action='index.php?process=submit'>
      <table border='1' align='center'>
        <tr>
         <td>Username</td>
         <td><input type='text' name='member_name' value='$member_name'></td>
        </tr>
        <tr>
         <td>Password</td>
         <td><input type='text' name='member_password' value='$member_password'></td>
        </tr>
        <tr>
         <td colspan='2' align='center'><input type='submit' name='submit' value='submit'></td>
        </tr>
     </table>
     </form>";
     
     $data=array('{content}' => $login_table);
     ReadTemplate('templates/main_temp.html', $data);
  }
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

You should do it with the variables that might not exist, for example all $_POST position.
The best way to fix this case is

Code: Select all

$process = isset($_POST['process']) ? $_POST['process'] : '';
$member_name = isset($_POST['member_name']) ? $_POST['member_name'] : '';
$member_password = isset($_POST['member_password']) ? $_POST['member_password'] : '';
So, after this, the 3 variables will be setted, having either the $_POST value or an empty string if they doesn't exist
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your logic is a little off. What you are doing in your original is saying:
If the POST var 'process' is set, make a new var called $process from its value. The problem is if the post var is not set then you have no $process var. So referencing it later will throw those errors. One approace is the suggested approach above this one. Another is to do something like:

Code: Select all

<?php
$process = '';
if (isset($_POST['process'])) {
  $process = $_POST['process'];
}
?>
Keep in mind that this is only a sample of logic and should be coupled with security checks, validation, sanitization and filtration along the way.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Everah wrote:

Code: Select all

<?php
$process = '';
if (isset($_POST['process'])) {
  $process = $_POST['process'];
}
?>
==
seppo0010 wrote:

Code: Select all

$process = isset($_POST['process']) ? $_POST['process'] : '';
:P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, they are the same. It's just that one is a little more readable to a new developer while the other is a faster and less line-intensive version. And if you have never encountered a ternary, then the second one would make your head spin for a bit.

;)
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Thanks for replying.

Code: Select all

switch($process)
   {
    case 'submit':
          submit($member_username, $member_password);
          break;
    
   
    case default:
         show_form('');
         break; 
   }
gives,
Parse error: parse error, unexpected T_DEFAULT in C:\wamp\www\message\index.php on line 16

if i try,

Code: Select all

case '':
         show_form('');
         break;
does not show the above error.



changed to,

Code: Select all

$process=isset($_GET['process']) ? $_GET['process']: '';
   $member_username=isset($_POST['member_username']) ? $_POST['member_username'] : '';
   $member_password=isset($_POST['member_password']) ? $_POST['member_password'] : '';
still getting,

Notice: Undefined variable: member_username in C:\wamp\www\message\index.php on line 46

Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 50


full code:

Code: Select all

<?php
   error_reporting(E_ALL);
   require_once('config/includes.php');
   
   $process=isset($_GET['process']) ? $_GET['process']: '';
   $member_username=isset($_POST['member_username']) ? $_POST['member_username'] : '';
   $member_password=isset($_POST['member_password']) ? $_POST['member_password'] : ''; 
   
   switch($process)
   {
    case 'submit':
          submit($member_username, $member_password);
          break;
    
   
    case '':
         show_form('');
         break; 
   }
   

  function submit($member_username, $member_password)
  {
    $query_check=mysql_query("SELECT * FROM members WHERE member_username=". "'$member_username'" . " AND member_password=". "'$member_password'");
    if(mysql_num_rows($query_check) > 0)
    {
      header("Location: ".SITE_URL."/members");
    }
    else
    {
      $message="Invalid login";
      show_form($message);
    }
     
  }
    
     
   
   function show_form($message)
   {
     $login_table=
     "<form method='post' action='index.php?process=submit'>
      <table border='1' align='center'>
        <tr>
         <td>Username</td>
         <td><input type='text' name='member_username' value='$member_username'></td>
        </tr>
        <tr>
         <td>Password</td>
         <td><input type='text' name='member_password' value='$member_password'></td>
        </tr>
        <tr>
         <td colspan='2' align='center'><input type='submit' name='submit' value='submit'></td>
        </tr>
     </table>
     </form>";
     
     $data=array('{heading}'=>'Login', '{content}' => $login_table, '{message}' => $message, '{links}' => '&nbsp;');
     ReadTemplate('templates/main_temp.html', $data);
  }
  

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

because it's just default

Code: Select all

switch ($foo) {
   case 'foobar';
      echo 'foobar';
   break;

   default :
      echo 'default';
}
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Thanks Jcart,

That error vanished.

How to solve,

Notice: Undefined variable: member_username in C:\wamp\www\message\index.php on line 46

Notice: Undefined variable: member_password in C:\wamp\www\message\index.php on line 50
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You need to pass $member_username to show_form(), since it is not in the same scope of when you initialized it.
Secondly, you should never re-diplay the users password :wink:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You really need to look at your code and think why $member_username and $member_password are not defined at that point in the code. Hint:

Code: Select all

function show_form($member_username, $member_password, $message)
(#10850)
Post Reply