登录功能与php mysql错误

时间:2014-04-02 11:37:13

标签: php mysql mysqli

我正在尝试使用php创建一个登录功能。

直到现在这就是我得到的。

<?php
include("dbconnection.php");
//select a database to work with
$mysqli->select_db("eamunter_opskrift");
Echo ("Selected the eamunter_opskrift database...<BR><BR>");
// get the username and password from the login form
$username=$_POST['username'];
$password=$_POST['password'];

//Protect username and password
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'";
$result = $mysqli->query($query);
$count=mysqli_num_rows($result);
// If result matched $username and $password, the count of table rows should equal 1
if($count==1){
// Register session variables and redirect the user to "login_success.php" page
session_start();
$_SESSION['username']= $username;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

我不断收到同样的错误 我无法弄清楚问题是什么......我的dbconnection.php可以在其他任何地方使用。

Selected the eamunter_opskrift database...

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 13

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 13

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 13

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 14

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 14

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 14

Wrong Username or Password

任何人都可以帮助我???

如果重要的话,我也可以上传表格,但并不认为这很重要。

4 个答案:

答案 0 :(得分:3)

您正在混淆mysql_*mysqli_*个功能。

应该是

$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);

答案 1 :(得分:3)

mysql_real_escape_string替换为$mysqli->real_escape_string

(另外,您不需要stripslashes次来电,real_escape_string应该为您完成所有这些操作)

答案 2 :(得分:1)

试一试:

<?php
    include("dbconnection.php");
    //select a database to work with
    $mysqli->select_db("eamunter_opskrift");
    Echo ("Selected the eamunter_opskrift database...<BR><BR>");
    // get the username and password from the login form
    $username=$_POST['username'];
    $password=$_POST['password'];

    //You should use your object and current connection 
    $username = $mysqli->real_escape_string($username);
    $password = $mysqli->real_escape_string($password);

    $query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'";
    $result = $mysqli->query($query);
    $count=mysqli_num_rows($result);
    // If result matched $username and $password, the count of table rows should equal 1
    if($count==1){
    // Register session variables and redirect the user to "login_success.php" page
    session_start();
    $_SESSION['username']= $username;
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

答案 3 :(得分:1)

请参阅mysql_real_escape_string

的文档

替换:

$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

by:

$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);