Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 08:06 PM   PM User | #1
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
Login error

Please assist:
I have created a database with a tb_user.

I have a form to checklogin.php with the code below:


The connect_db.php
Code:
<?PHP
// I am setting all my variables to make it easier to change code at a later stage by just cganging the variables
$connect_error = "Could not connect to Database";
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_passw = "";
$mysql_db = "games";

// Using a f statement with the not inside that if the DB does not exist or could not connect
// it will die with an error message as per the variable, I used the @ sign to silence the normal error message
if (!@mysql_connect($mysql_host, $mysql_user, $mysql_passw) OR !@mysql_select_db($mysql_db)) {
	die($connect_error);
	}

?>
The checklogin.php
Code:
<?PHP
// Import the connect to DB code as required as we need it and can not continue with out it.
require 'connect_db.php';

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// To protect MySQL injection
 $myusername = stripslashes($myusername);
 $mypassword = stripslashes($mypassword);
 $myusername = mysql_real_escape_string($myusername);
 $mypassword = mysql_real_escape_string($mypassword);

// Table name variable
$tb_name = "tb_user";

// Set the Query within a variable
$query = "SELECT * FROM $tb_name WHERE username='$username' AND password='$password'";
$result = mysql_query($query);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);

// Create a if statement to check if results returned and if not to display an error
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 "Username or Password INCORRECT!";
 }
	
?>
It connects to the DataBase however it gives the following error:

( ! ) Notice: Undefined variable: username in C:\wamp\www\exam\checklogin.php on line 19
Call Stack
# Time Memory Function Location
1 0.0022 143480 {main}( ) ..\checklogin.php:0

( ! ) Notice: Undefined variable: password in C:\wamp\www\exam\checklogin.php on line 19
Call Stack
# Time Memory Function Location
1 0.0022 143480 {main}( ) ..\checklogin.php:0
Username or Password INCORRECT!

username and password both do exist in my DataBase in column 5 and 6.

Pulling my hair here I am sure this must work.
hans_cellc is offline   Reply With Quote
Old 10-09-2012, 08:21 PM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
Code:
$query = "SELECT * FROM $tb_name WHERE username='$username' AND password='$password'";
You're putting the entered values in $myusername and $mypassword. Shouldn't this be:

Code:
$query = "SELECT * FROM $tb_name WHERE username='$myusername' AND password='$mypassword'";
Dave
tracknut is online now   Reply With Quote
Users who have thanked tracknut for this post:
hans_cellc (10-09-2012)
Old 10-09-2012, 08:32 PM   PM User | #3
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
Absolutely correct, I feel so STUPID. But have learnt an important lesson.

Thanks a lot.
hans_cellc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:47 PM.


Advertisement
Log in to turn off these ads.