Page 1 of 1
please fix the error
Posted: Thu Oct 22, 2009 7:51 am
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']);
}
}
?>
Re: please fix the error
Posted: Thu Oct 22, 2009 7:55 am
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'.
Re: please fix the error
Posted: Thu Oct 22, 2009 8:05 am
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...
Re: please fix the error
Posted: Thu Oct 22, 2009 8:11 am
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?
Re: please fix the error
Posted: Thu Oct 22, 2009 8:21 am
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..
Re: please fix the error
Posted: Thu Oct 22, 2009 8:36 am
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
Re: please fix the error
Posted: Thu Oct 22, 2009 8:39 am
by markusn00b
After you run the MySQL query, do a:
to see what MySQL is complaining about.