I am new in php and I have created a php login page where it will retrive data from a table and check a print "login successful" or "login failed". Now after execution of the codeI am giving correct data but all the time it is replying "login failed" plese help me

<?php

$hostname = 'localhost';
$dbname = 'phplog';
$username = 'root';
$password = '';

mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);

$use = $_POST['data'];
$use = stripslashes($use);
$use = mysql_real_escape_string($use);
$get = "SELECT * FROM user WHERE username='$use'";
$set = mysql_query($get);
$row = mysql_num_rows($set);
if($row == 1){
print("login successful");
}
else{
print("Login failed");
}

?>

Recommended Answers

All 23 Replies

Try this and tell us if you get an error:

$set = mysql_query($get) or die(mysql_error());

14.
$get = "SELECT * FROM user WHERE username='$use'";
15.$set = mysql_query($get);
16.$row = mysql_num_rows($set);
17.if($row == 1)

Store it in a $_SESSION, otherwise I don't think it's being fired.

if(mysql_num_rows($result)==0)
{
echo "ERROR - No mactching rows from the database!<br/>";
}
else
{   
$_SESSION['ADMIN'] = $_POST["admin"];
}

Then you will be able to use it globally just by adding session_start(); at the beginning of the page.

Hello,pls check the username field in the table must be unique
and in the condition may intiate $row=0; and use "$row >= 1" instead of "$row == 1",
it should worked:

<?php
$hostname = 'localhost';
$dbname = 'phplog';
$username = 'root';
$password = '';
mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);
$use = $_POST['data'];
$use = stripslashes($use);
$use = mysql_real_escape_string($use);
$get = "SELECT * FROM user WHERE username='$use'";
$set = mysql_query($get);
$row=0;
$row = mysql_num_rows($set);
if($row >= 1){
print("login successful");
}
else{
print("Login failed");
}
?>

.
%%^Murtada^%%

If you have a question, post a thread, containing both your ideas and code , otherwise there isn't any point stealing someon else's thread.

<?php
$submit = @$_POST['submit'];
//form data
$fullname = strip_tags (@$_POST['fullname']);
$username = strip_tags (@$_POST['username']);
$password= strip_tags(@$_POST['password']);
$repeatpassword = strip_tags(@$_POST['repeatpassword']);
$date =date("Y-m-d");
if ($submit)
{
// check for existance
if($fullname&&$username&&$password&&$repeatpassword)
{

if($password==$repeatpassword)
{

//chechk char length of username and fullname
if (strlen($username)>25||strlen ($fullname)>25)
{
echo ("Length of username or fullname is too long!");
}
else
{
//check password length
if(strlen($password)>25||strlen ($password)<6)
{
 die ("Password must be  between 6 and 25 characters");
}
else
{
//register the user!

//encrypt password
$password = md5($password);
$repeatpassword = md5($repeatpassword);
// open database
$connect = mysql_connect("localhost","root","");
mysql_select_db("phplogin");  // select database

$queryreg =mysql_query("
INSERT INTO users VALUES ('','$fullname','$username','$password','$date',)
");
die ("You have been registered!<a href='index.php'> Return to Login page</a>");

}


}

}
else
 die ("Your password do nat match!");
}
else
 die("Please fill in <b>all</b> fields!");


}
?>

<body>
<form action="login.php" method="post">
Username: <input type='text' name ='username'><br>
Password: <input type="password" name='password'><br>
<input type="submit" value="Log in">
</form>

</body>

i wont to make REGISTER as link in the second table,so it can send me in register page,can you help me

Hello andyy121,I think this code above it's for sign up not login.but it can be usefull

i wont to connec this two pages.example: when i click Register in the html pages it can lead me in Register in php pages how i can do that

pls,give me amoment,,,,

check this code:

<form action="" method="post" >
name:<input name="fullname" size="25">
username:<input name="username" size="25">
password:<input type="password" name="password" size="25">
repeatpassword:<input type="password"  name="repeatpassword" size="25">
<input type="submit" name="submit" value="Register">

</form>

<?php
if(isset($_POST['submit']))
{
//form data
$fullname = strip_tags (@$_POST['fullname']);
$username = strip_tags (@$_POST['username']);
$password= strip_tags(@$_POST['password']);
$repeatpassword = strip_tags(@$_POST['repeatpassword']);
$date =date("Y-m-d");
if ($submit)
{
// check for existance
if($fullname&&$username&&$password&&$repeatpassword)
{
if($password==$repeatpassword)
{
//chechk char length of username and fullname
if (strlen($username)>25||strlen ($fullname)>25)
{
echo ("Length of username or fullname is too long!");
}
else
{
//check password length
if(strlen($password)>25||strlen ($password)<6)
{
die ("Password must be between 6 and 25 characters");
}
else
{
//register the user!
//encrypt password
$password = md5($password);
//$repeatpassword = md5($repeatpassword);
// open database
$connect = mysql_connect("localhost","root","");
mysql_select_db("phplogin"); // select database
$queryreg =mysql_query("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')") ;
//++++++++++new lines++++++++++++++++++++++++
if($queryreg)
{
echo "<script>alert('".$alrt13."');</script>";    
echo "<meta http-equiv='refresh' content='0; url=index.php' >";//too login page 
}
}
}
}
else
die ("Your password do nat match!");
}
else
die("Please fill in <b>all</bfields!");
}
}
?>

I added this conditon to check if submition is done:

if(isset($_POST['submit']))

$set = mysql_query($get) or die(mysql_error());

I didn't get any error message but still it print "Login failed"

try to comment that line :

$use = stripslashes($use);

if same error happands again, pls test the connection then test your sql statement,pls give the structure of user table ,to help you.

Notice: Undefined variable: submit in C:\xampp\htdocs\site 2\register.php on line 19

it show me that

before line 19 add this line:

$submit=$_POST['submit'];

listen forget all this i just wont to make the REGISTER in link whcih it is ia the html page and it can lead me to register page

okey it's easy:
this link in the home page okay:

<a href="register.php" >Registeration</a>

do you mean that!!

the code of html , and php they can be in one page 'register.php'

Field - username
value - login

????

It the the field name and the value in that field of the table user in the above code which I need help. Pls someone try to execute the above code in your computer and tell me wheather you are getting same error or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.