Login Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Login Code

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Login Code

Post by Jade »

Either you don't have a database with that name or your db login failed.
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Login Code

Post 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 ^^
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Login Code

Post by oscardog »

Hello,

In checklogin.php change

Code: Select all

$db_name="test"; // Database name 
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 
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Login Code

Post by emilcarlo »

oscardog wrote:Hello,

In checklogin.php change

Code: Select all

$db_name="test"; // Database name 
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
Last edited by Benjamin on Tue Jul 27, 2010 9:13 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Login Code

Post 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";
}
?>
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Login Code

Post 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");
?>
Last edited by Benjamin on Tue Jul 27, 2010 9:13 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
emilcarlo
Forum Commoner
Posts: 43
Joined: Wed Jul 21, 2010 12:38 pm

Re: Login Code

Post by emilcarlo »

Thanks peeps for all the responses, login code working now ^^
Post Reply