-3

I get this error when i run my checklogin.php:

Notice: Undefined variable: username in C:\xampp\htdocs\checklogin.php on line 4

Notice: Undefined variable: password in C:\xampp\htdocs\checklogin.php on line 4

Notice: Undefined variable: db_name in C:\xampp\htdocs\checklogin.php on line 4
cannot select DB

here's my code for the checklogin.php

<?php

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// 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";
}

I can't really see what's wrong here. I'm sorry if it's obvious or lack any details as I'm really new to this.

MrTux
  • 32,350
  • 30
  • 109
  • 146
lasselre
  • 3
  • 4
  • 1
    This is not error,it's just a notice. – Bhumi Shah Nov 10 '14 at 09:52
  • 4
    [Please, stop using mysql_* functions](http://stackoverflow.com/q/12859942/1238019) in new code, they are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Instead of, have a look on [prepared statements](http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html), and use [Mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php). – zessx Nov 10 '14 at 09:53
  • 1
    The code you post does not coincide with the output. The messages supposedly come from line 4. – Joni Nov 10 '14 at 09:54
  • I have a feeling that you've adjusted your code for the question since it says your error is on line 4 but `mysql_connect` is called around line 10. Your code above seems to be fine, though you don't need to put quotes around your variables when calling `mysql_connect`. Also, stop using MySQL. Use PDO or MySQLi instead. – Jonathon Nov 10 '14 at 09:55
  • all those variables are defined there.... hmm.... – itachi Nov 10 '14 at 09:56

3 Answers3

0

In your code second line put username as root

$username="root";
Ravi Sukhadia
  • 443
  • 6
  • 16
0

I cannot see the relation between the notice you got, and the source code provided.

Well, to handle this notice the next time :

1- If you are not sure if the variable is declared use :

<?php
if(isset($variable_name))
{
   print $variable_name;
}

2- Or you can avoid notices by using error_reporting function, in your case see line 2 and 3:

<?php
// Report all errors except E_NOTICE   
error_reporting(E_ALL ^ E_NOTICE);

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// 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");
Hamza
  • 1,087
  • 4
  • 21
  • 47
0
<?php
class DB extends PDO { 

    private $engine; 
    private $host; 
    private $database; 
    private $user; 
    private $pass;      

      private static $instance = NULL;

    public function __construct(){           
        $this->engine = 'mysql'; 
        $this->host = 'localhost'; 
        $this->database = 'test'; //your
        $this->user = 'root'; //your
        $this->pass = '123'; //your
        $dns = $this->engine.':dbname='.$this->database.";host=".$this->host; 
        parent::__construct( $dns, $this->user, $this->pass);       
        $this::__config();      
    }


     static function getInstance()
    {
        if (self::$instance == NULL)
            self::$instance = new DB();
        return self::$instance;
    }


    public function __config(){ //there your config - it's my 
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        $this->query("set character_set_client='utf8'");
        $this->query("set character_set_results='utf8'");      
        $this->query ("SET NAMES utf8");       
    }


} 
?>

use this class in your project with PDO $db = db::getInstance(); //call

Gedzberg Alex
  • 529
  • 4
  • 5