please fix the error

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
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

please fix the error

Post by lipun4u »

What is the error in the below code ???

Code: Select all

 
<?php
         if(array_key_exists_r('email|password', $_POST)) {
            include_once('database.php');
            $sql = "SELECT user_id FROM login WHERE user_name = '" . trim($_POST['email']) . 
               "' AND password='" . trim($_POST['password']) . "')";
             $result = mysql_query($sql);  
            print_r($result);
             if (mysql_num_rows($result)>0){  
                $row = mysql_fetch_row($result);  
               session_start();    
               $_SESSION['user_id']=$row[0];  
               $_SESSION['user_name']=trim($_POST['email']);
            }
         }
?>
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: please fix the error

Post by markusn00b »

If you want us to help you, you have got to give a lot more information on your problem.

This time, though, I'll go ahead and say that you're calling print_r() (which sends output to your browser) and then you are calling session_start(). That combination will give you a fatal error of 'headers already sent'.
Last edited by markusn00b on Thu Oct 22, 2009 8:16 am, edited 1 time in total.
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: please fix the error

Post by lipun4u »

Actually I have an error in this line

Code: Select all

 
$_SESSION['user_name']=trim($_POST['email']);
I have commented the line print_r()(which was for debugging purpose), still the error persists...
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: please fix the error

Post by markusn00b »

lipun4u wrote:Actually I have an error in this line

Code: Select all

 
$_SESSION['user_name']=trim($_POST['email']);
I have commented the line print_r()(which was for debugging purpose), still the error persists...
Again... what error?
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: please fix the error

Post by lipun4u »

The strip down code is

Code: Select all

 
<?php
include_once("mylib.php");
?>
<head>
    <title>Login</title>
    
</head>
<body>
   
            <?php
            if(array_key_exists_r('email|password', $_POST)) {
                include_once('database.php');
                $sql = "SELECT user_id FROM login WHERE user_name = '" . trim($_POST['email']) . 
                    "' AND password='" . trim($_POST['password']) . "')";
                $result = mysql_query($sql);  
                //print_r($result);
                if (mysql_num_rows($result)>0){  
                    $row = mysql_fetch_row($result);  
                    session_start();    
                    $_SESSION['user_id']=$row[0];  
                    $_SESSION['user_name']=trim($_POST['email']);
                }
            }
            else {
            ?>
                <form id="example" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                    <div>
                    <table>
                    <tr><td align="right">E-mail </td>
                    <td align="left"><input type="text" class="textbox"  id="email" name="email" /></td></tr>
                    <tr><td align="right">Password</td>
                    <td align="left"><input type="password" class="textbox"  id="password" name="password" /></td></tr>
                    <tr><td align="right"><input type="submit" value="Login" name="submit" class="button" /></td>
                    <td align="left"><input name="reset" type="reset" class="button" value="Clear" /></td></tr>
                    </table>
                    </div>
                </form>
            <?php
            }
            ?>
            
</body>
</html>
mylib.php contains some function defination and database.php contains code to connect database.

Now the error turned to warning....
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in F:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php-forum.php on line 17
Some body please help me..
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: please fix the error

Post by Mark Baker »

The error is actually in this line:

Code: Select all

               $sql = "SELECT user_id FROM login WHERE user_name = '" . trim($_POST['email']) . 
                    "' AND password='" . trim($_POST['password']) . "')";
echo $sql immediately after this to see what your database query looks like, and count the opening and closing brackets
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: please fix the error

Post by markusn00b »

After you run the MySQL query, do a:

Code: Select all

 
print mysql_error();
 
to see what MySQL is complaining about.
Post Reply