Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

unable to login in php mysql

Codes here !

Moderators: macek, egami, gesf

unable to login in php mysql

Postby taimoor » Tue Jul 31, 2012 8:02 am

here is my html code

Code: Select all
 <form action="login.php" method ="post" onsubmit="return validate (this);"> <div style="clear:both; font-family:Verdana, Geneva, sans-serif; font-size:10px; color:#333;">Username</div>
        <input name="username" type="text" id="name" style="width:300px; border:1px; border-style:solid; border-color:#969696; height:20px; margin-bottom:8px; clear:left;" />
        <div style="clear:both; font-family:Verdana, arial; font-size:10px; color:#333;"> Password </div>
        <input name="password" type="password" id="password" style="width:300px; border:1px; border-style:solid; border-color:#969696; height:20px; margin-bottom:8px; clear:left;"/>
<div style="margin-bottom:8px; clear:both; width:300px; font-family:Verdana, Geneva, sans-serif; font-size:10px; color:#333; text-align:right;"><span style="clear:both; font-family:Verdana, Geneva, sans-serif; font-size:10px; color:#333;"></span><span><input type="submit" name="submit" value="LOG IN" style="background-color:#090; color:#FFF; border:none; cursor:pointer"  /> </span></div>
</form><br />



and here is my login.php code

Code: Select all
<?php

$host="localhost";
$username="root"; 
$password="";
$db_name="transportdb";



$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//echo "connected <br/>";
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM user WHERE LoginName='$myusername' and Password='$mypassword'";
//echo "$sql <br/>";
$result=mysql_query($sql,$con) or die ('Unable to run query:'.mysql_error());
$count=mysql_num_rows($result);
if($count==1){

session_register("myusername");
session_register("mypassword");
header("location:home.php");
}
else {
echo "Wrong Username or Password";
}
?>



here is the error i am getting
Unable to run query:Table 'transportdb.user' doesn't exist



I am stuck with this because //echo "connected <br/>"; and //echo "$sql <br/>"; successfully executed but i think i have problem in section
$result=mysql_query($sql,$con) or die ('Unable to run query:'.mysql_error());
$count=mysql_num_rows($result);
if($count==1){

session_register("myusername");
session_register("mypassword");
header("location:home.php");
}
else {
echo "Wrong Username or Password";
}
?>
taimoor
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Jul 31, 2012 12:03 am

Re: unable to login in php mysql

Postby iam4423 » Tue Jul 31, 2012 8:54 am

i dont think it will be causing your problem but you dont need
Code: Select all
mysql_query($sql,$con)


Code: Select all
mysql_query($sql)
will get the job done!

there are a few things i would try to resolve this issue firstly put your table and field names inside ` `
Code: Select all
$sql="SELECT * FROM `user` WHERE `LoginName`='$myusername' AND `Password`='$mypassword'";

and if all else fails extract your query from phpmyadmin and then just add your php variables to that. the amount of times ive not been able to work out what was wrong with my query and ive extracted it and still not been able to tell the difference but its worked anyway!
iam4423
New php-forum User
New php-forum User
 
Posts: 50
Joined: Fri Jan 20, 2012 8:00 am

Re: unable to login in php mysql

Postby freshnet » Thu Aug 02, 2012 1:29 pm

The first thing to do is echo back your query and put that into phpmyadmin to see if it runs. Usually that will give you the error, if it isn't obvious just from looking at the echoed query.

Also, you should switch from using mysql to mysqli/PDO as mysql is not secure.
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 277
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: unable to login in php mysql

Postby NigelRen » Thu Aug 02, 2012 11:24 pm

The error your getting is quite specific - the table doesn't exist. Have a look at your database and check that the there is in fact a table called 'user' in the schema you've specified.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron