php查询错误插入

时间:2015-06-25 17:50:13

标签: php mysqli

<?php
$username = "...";
$password = "...";
$database = "...";



switch($_POST['function'])
{
    case 'register':
        $db_handle = new mysqli('localhost',$username,$password,$database);
        $sql = "SELECT * FROM `GameSaves` WHERE `Username` = '$un' and `Password` = '$pw'";

        if (!$db_handle->connect_error)
        {
            $r = $db_handle->query($sql);
            $count= $r->num_rows;

            $un = $_POST['username'];
            $pw = $_POST['password'];
            $data = '0';

            if ($count == 1)
                echo "fail";
            else
            {
                $sql = "INSERT INTO `GameSaves` (Username,Password,SaveData) VALUES ('test1','test2','0')";
                $res = $dbl_handle->query($sql);
                echo "succes";
            }
        }
        mysqli_close($db_handle);
    break;

    case 'login':
        $un = $_POST['username'];
        $pw = $_POST['password'];

        $db_handle = new mysqli('localhost',$username,$password,$database);
        $sql = "SELECT * FROM `GameSaves` WHERE `Username` = '$un' and `Password` = '$pw'";

        if (!$db_handle->connect_error)
        {
            $r = $db_handle->query($sql);
            $count= $r->num_rows;

            if ($count == 1)
                echo "succes";
            else
                echo "fail";
        }
        mysqli_close($db_handle);
    break;

    case 'getdata':
        $un = $_POST['username'];
        $sql = "SELECT * FROM `GameSaves` WHERE `Username` = '$un'";
        $db_handle = new mysqli('localhost',$username,$password,$database);

        if (!$db_handle -> connect_error)
        {
            $retval = $db_handle->query($sql);

            while ($row = $retval->fetch_assoc())
            {
                echo "{$row['SaveData']}";
            }
            mysqli_close($db_handle);
        }
    break;

    case 'savedata':
        $un = $_POST['username'];
        $db_handle = new mysqli('localhost',$username,$password,$database);

        if (!$db_handle -> connect_error)
        {   
            $data = $_POST['data'];
            $retval = $db_handle->query("UPDATE `GameSaves` SET `SaveData` = '$data' WHERE `Username` = '$un'");
            mysqli_close($db_handle);
        }
    break;
}
?>

这是我的班级。

我收到错误:在第28行的非对象上调用成员函数query()。 我无法在寄存器情况下在$ sql上更改什么。 我在互联网上无处不在,但我无法找到解决问题的方法。

2 个答案:

答案 0 :(得分:1)

您的代码中存在拼写错误。

变化

$dbl_handle->query($sql);

$db_handle->query($sql);

答案 1 :(得分:0)

这是

的问题
 $db_handle = new mysqli('localhost',$username,$password,$database);

您没有收到mysqli对象,因此您不能使用mysqli :: query()方法。

更改

if (!$db_handle->connect_error)

if (!$db_handle->connect_errno)

因为您可能无法使用&#34; connect_error&#34;正确处理连接错误。

你确定变量

$username,$password,$database

有适当的访问数据吗?