运行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
这是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";
}
我真的不明白这里有什么问题。我很抱歉,如果它显而易见或缺乏任何细节,因为我对此很陌生。
答案 0 :(得分:0)
在您的代码第二行中,将用户名设为root
$username="root";
答案 1 :(得分:0)
我看不到您收到的通知与提供的源代码之间的关系。
嗯,下次要处理此通知:
1-如果您不确定是否声明变量,请使用:
<?php
if(isset($variable_name))
{
print $variable_name;
}
2-或者您可以通过使用error_reporting函数来避免通知,在您的情况下,请参阅第2行和第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");
答案 2 :(得分: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");
}
}
?>
在PDO项目中使用此类 $ db = db :: getInstance(); //致电