Jump to content

[SOLVED] Redirecting of page in php username password page


overidemehra

Recommended Posts

Hi there,

 

I have just started to learn php, and i wanted to learn how to make the username & password page for ppl to login in and when they click login, it should take them to another page.

 

I got up to how to connect to the database with sql server and to make the username and password fields. But the problem is that when i click on the login button it does not go anywhere. My sql database has one table (login), in which i have a 3 rows, named ID, L1 (for username) and L2 (for password) and at the moment only one username and password is stored.

 

At the moment when i run this page, it gives me the follwoing error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/contentxxxxxxxxx.../login.php on line 47

 

i dont understand what this means, at times num_row works, sometime sit does not. All this i have got from an example i have learnt till now online.

 

I hope someone can help me out here

 

:)

 

heres my php script

 

<?php session_start(); ?>

<html>

<head>

<title>LOGIN</title>

</head>

<body>

 

<FORM NAME ="form1" METHOD ="POST" ACTION ="login.php">

 

Username: <input type=text name=user><br />

Password: <input type=password name=pass><br />

 

<P align = left>

<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Login">

</P>

 

</FORM><?php

 

 

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$uname = $_POST['username'];

$pword = $_POST['password'];

 

$uname = htmlspecialchars($uname);

$pword = htmlspecialchars($pword);

 

//==========================================

// CONNECT TO THE LOCAL DATABASE

//==========================================

$user_name = "XXXXX";

$pass_word = "XXXXXX";

$dbname="XXXXX";

$table = "login";

$server = "XXXX";

 

$c=mysql_connect($server,$user_name, $pass_word) OR DIE ('Unable to connect to database! Please try again later.');

$db_found=mysql_select_db($dbname);

 

 

 

 

 

if ($db_found) {

 

$query = "SELECT * FROM $table WHERE L1 = $uname AND L2 = md5($pword)";

$result = mysql_query($query);

        $num_rows = mysql_num_rows($result);

 

 

//====================================================

// CHECK TO SEE IF THE $result VARIABLE IS TRUE

//====================================================

 

 

if ($result) {

$num_rows = mysql_num_rows($result); {

session_start();

$_SESSION['login'] = "1";

function redirect($url) {

if (!headers_sent())

header('location:http://xxxxxxx/page1.php'.$url);

else {

echo "HEADER ALREADY SENT";

}

}

 

}

 

}

else {

$errorMessage = "Error logging on";

}

 

 

}

 

else {

$errorMessage = "Error logging on";

}

 

}?>

</body>

</html>

hi,

 

 

thanks for the replies :) , i tried doing what u all said, but it did not work out and i went ahead reading online  other ppls problem and this is what i did and it works now without any error, but one big PROBLEM  is that, if i click login with any username pass or even keeping it blank it goes through the other page which indented it to go to.

 

this is my script..

 

 

 

<?php

$action=$_GET["action"];

if($action=="login")

{

$uname = $_POST["user"];

$pword = $_POST["pass"];

 

//==========================================

// CONNECT TO THE LOCAL DATABASE

//==========================================

$user_name = "xxxxx";

$pass_word = "xxxxx";

$dbname="xxxxx";

$table = "login";

$server = "xxxx";

 

$c=mysql_connect($server,$user_name, $pass_word, $table) OR DIE ('Unable to connect to database! Please try again later.');

$db_found=mysql_select_db($dbname);

 

if ($db_found)

{

 

$query = "SELECT * FROM `$table` WHERE `username` = '$uname' AND `password` = '$pword'";

$result = mysql_query($query);

//$result = mysql_num_rows($result);

     

if ($result)

              {

                      ?>

                      Login Sucessful! <A href="page1.php">Click here to proceed</a>

                      <?

              }

else

{

echo "Error logging on\n";

}

}

else

{

echo "Error connecting to db\n";

}

}

else if(!$action)

{

?>

<html>

<head>

<title>LOGIN</title>

</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION ="login.php?action=login">

 

Username: <input type=text name="user" id="user"><br />

Password: <input type=password name="pass" id="pass"><br />

 

<P align = left>

<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Login">

</P>

 

</FORM>

</body>

</html>

<?

}

?>

 

 

ok i got how to fix that.... but now i get a warning message on the login error page ...

 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home/content/xxxxxx/login.php on line 26

 

 

i did  after coonection lines

 

 

if ($db_found)

{

 

$query = "SELECT username FROM `$table` WHERE `username` = '$uname' AND `password` = '$pword'";

$result = mysql_query($query);

if ($result)

$result = mysql_result($result,0);

     

if ($result == $uname)

              {

                      ?>

                      Login Sucessful! <A href="page1.php">Click here to proceed</a>

                      <?

              }

else

{

echo "Error logging on\n";

}

}

else

{

echo "Error connecting to db\n";

}

}

else if(!$action)

{

?>

<html>

<head>

<title>LOGIN</title>

</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION ="login.php?action=login">

 

Username: <input type=text name="user" id="user"><br />

Password: <input type=password name="pass" id="pass"><br />

 

<P align = left>

<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Login">

</P>

 

</FORM>

</body>

</html>

<?

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.