Page 1 of 1
Login Code
Posted: Thu Jul 22, 2010 11:03 am
by emilcarlo
Hi again ^^
Good evening,
I am trying to create a login page which shall validate the users' username and password. Since I am still learning PHP, and is still in the process of understanding codes, I use instant tutorials. I was brought in this website
http://www.phpeasystep.com/phptu/6.html which provides a step by step tutorial. I did the procedure and it worked, given that I haven't changed any, from database to variables. The problem is, when I tried to change the database name, an error message "cannot select DB" is appearing. Can anyone help me with this?
Thanks in advanced.
Re: Login Code
Posted: Thu Jul 22, 2010 1:36 pm
by Jade
Either you don't have a database with that name or your db login failed.
Re: Login Code
Posted: Thu Jul 22, 2010 1:51 pm
by emilcarlo
Jade wrote:Either you don't have a database with that name or your db login failed.
Hi, thanks for the reply.
I did have the database created through PHP myadmin, and I was wondering why I cannot retrieve database information if my database name is different from test. You said about db login failure, how does this happen?
Thanks again, newbie here ^^
Re: Login Code
Posted: Thu Jul 22, 2010 3:10 pm
by oscardog
Hello,
In checklogin.php change
And instead of "test" put "your-database-name-that-you-created-in-phpmyadmin-between-the-quotes".
So if your database name was login_details it would look like:
Code: Select all
$db_name="login_details"; // Database name
Re: Login Code
Posted: Thu Jul 22, 2010 3:43 pm
by emilcarlo
oscardog wrote:Hello,
In checklogin.php change
And instead of "test" put "your-database-name-that-you-created-in-phpmyadmin-between-the-quotes".
So if your database name was login_details it would look like:
Code: Select all
$db_name="login_details"; // Database name
I already did this one too, and I even started from scratch and then just change the values on checklogin.php, and then create database according to the checklogin.php information. Still having the error.
Here is the code:
Code: Select all
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name - this one was changed to test2
$tbl_name="members"; // Table name - this one was changed to users
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Nothing was changed there, just the dbname, and tablename.
Note: I alreadt have the dbname test2 and tablename users on the database, but I still get the error T_T
Re: Login Code
Posted: Fri Jul 23, 2010 10:41 am
by Jade
Try this, you'll get a more detailed error message:
Code: Select all
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name - this one was changed to test2
$tbl_name="members"; // Table name - this one was changed to users
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect: " . mysql_error());
mysql_select_db("$db_name")or die("cannot select DB: " . mysql_error());
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql) or die (mysql_error());
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Re: Login Code
Posted: Fri Jul 23, 2010 12:17 pm
by katierosy
Please try with these two lines of code
Code: Select all
<?php
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
?>
Re: Login Code
Posted: Tue Jul 27, 2010 8:58 am
by emilcarlo
Thanks peeps for all the responses, login code working now ^^