注意:未定义的变量:第63行的......中的mysqli

时间:2012-05-07 13:01:18

标签: php ajax mysqli

我收到此错误我真的不知道该怎么办。 我正在构建一个“表单”,您可以使用该表单向数据库添加信息。 因为我想在我的数据库中没有双重记录,所以我用AJAX检查了一些字段。

在这里,您可以看到我的班级代码,我收到了错误。 (我使用mysqli - 语言)

<?php
class Places {

    private $m_sName;
    private $m_sStreet;
    private $m_sHouseNumber;
    private $m_sCity;
    private $m_sCategory;

    public function __set($p_sProperty, $p_vValue) {
        switch($p_sProperty) {
            case "Name" :
                $this -> m_sName = $p_vValue;
                break;
            case "Street" :
                $this -> m_sStreet = $p_vValue;
                break;
            case "HouseNumber" :
                $this -> m_sHouseNumber= $p_vValue;
                break;
            case "City" :
                $this -> m_sCity = $p_vValue;
                break;
            case "Category" :
                $this -> m_sCategory = $p_vValue;
                break;
        }
    }

    public function __get($p_sProperty) {
        $vResult = null;
        switch($p_sProperty) {
            case "Name" :
                $vResult = $this -> m_sName;
                break;
            case "Street" :
                $vResult = $this -> m_sStreet;
                break;
            case "HouseNumber" :
                $vResult = $this -> m_sHouseNumber;
                break;
            case "City" :
                $vResult = $this -> m_sCity;
                break;
            case "Category" :
                $vResult = $this -> m_sCategory;
                break;
        }
        return $vResult;
    }

    public function addPlaces() 
    {
        include_once("connection.php");
            $sSql = "INSERT INTO tblPlaces
                (Name, 
                Street, 
                HouseNumber, 
                City, 
                Category) 
                VALUES 
                ('" . $mysqli -> real_escape_string($this -> m_sName) . "', 
                '" . $mysqli -> real_escape_string($this -> m_sStreet) . "', 
                '" . $mysqli -> real_escape_string($this -> m_sHouseNumber) . "', 
                '" . $mysqli -> real_escape_string($this -> m_sCity) . "', 
                '" . $mysqli -> real_escape_string($this -> m_sCategory) . "')";


        if (!$mysqli -> query($sSql))
        {
            throw new Exception("Er is iets mis gelopen bij het toevoegen van een plaats");
        }

    }

    public function placeAvailable()
    {

        include("connection.php");

        $sSql= "select Street from tblPlaces
                where Street = '".$this->m_sStreet."' AND HouseNumber = '".$this->m_sHouseNumber."'";

        $vResult=$mysqli->query($sSql);
        if($vResult->num_rows>0)
        {

            return(false);  
        }
        else
        {

            return(true);
        }

        $mysqli->close();   
    }
}
?>

在我的连接文件中,我有这段代码:

<?php

$localhost = "localhost";
$user = //user hidden
$password = //paswoord hidden 
$database = //database hidden

$mysqli = new mysqli($localhost, $user, $password,$database);

if ($mysqli->connect_error) {
    throw new Exception("Geen Databankconnectie");
}
?>

有没有人有解决方案?或者你也想看到我的ajax文件和.php页面? 感谢

修改

这是我的add.php文件(至少重要的是一切)

<?php
$feedback = "";
include_once ('assets/classes/places.class.php');
if (isset($_POST['knop'])) {
    try 
    {
        $place1 = new Places;
        $place1 -> Name = $_POST['Name'];
        $place1 -> Street = $_POST['Street'];
        $place1 -> HouseNumber = $_POST['HouseNumber'];
        $place1 -> City = $_POST['City'];
        $place1 -> Category = $_POST['Category'];

            if($place1->placeAvailable())
            {
                $place1 -> addPlaces();
                $feedback = $place1 -> Name . ", is met succes toegevoegd!";
            }
            else
            {
                $feedback = "Sorry";            
            }
    } 
    catch (Exception $e) 
    {
        $feedback = $e -> getMessage();
    }
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8" />
        <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
        Remove this if you use the .htaccess -->
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>Search | FoodSquare</title>
        <link rel="stylesheet" href="assets/css/reset.css" />
        <link rel="stylesheet" href="assets/css/style.css" />
        <script type="text/javascript" src="assets/javascript/geolocation.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

        <meta name="description" content="" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0" />
        <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
        <link rel="shortcut icon" href="/favicon.ico" />
        <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            $("#klik").click(function(){
            var naam = $("#naam").val();
            var plaats = $("#straat").val();
            var nummer = $("#huisnummer").val();
            var block = "block";

        $.ajax({
            type: "POST",
            url: "assets/ajax/check_place.php",
            data: { eet:naam, place:plaats, number:nummer }
        }).done(function( msg ) {

            if(msg.status != "error")

                {
                    if(msg.available == "yes")
                    {
                        $(".feedback").fadeOut();
                    }
                    else
                    {
                        $(".feedback span").text(msg.message);
                        $(".feedback").fadeIn();
                        $(".feedback").css("display", block);
                    }
                }

        });
        return(false);

    })

});
</script>
    </head>
    <body>

这是我的ajax文件

<?php
ini_set('display_errors', 1);
    include_once('../classes/places.class.php');
try
{
    $oPlace = new Places();
    $oPlace->Name = $_POST['eet'];
    $oPlace->Street = $_POST['place'];
    $oPlace->HouseNumber = $_POST['number'];
    if($oPlace->placeAvailable())
    {
        $feedback['status'] = "success";
        $feedback['available'] = "yes";
        $feedback["message"] = "Go ahead, street is available";
    }   
    else
    {
        $feedback['status'] = "success";
        $feedback['available'] = "no";
        $feedback["message"] ="De zaak " . "'" . $_POST['eet'] . "'". " is reeds op dit adres gevestigd." ;
    }
}
catch(exception $e)
{
    $feedback['status'] = "error";
    $feedback["message"] =$e->getMessage();

}
header('Content-type: application/json');
echo json_encode($feedback);
?>

好的,我尝试了几种解决方案,但没有一种方法可行。我可能是我的错,但我想出了一些东西,我用INCLUDE替换INCLUDE_ONCE,现在我可以添加一些东西到我的数据库但只有没有AJAX,当我使用AJAX时,表单检查值是否已经在数据库中但是当我添加它们,没有任何反应。我也没有收到任何错误。我也从ajax收到了正确的信息。有人可以帮忙吗?谢谢

4 个答案:

答案 0 :(得分:3)

您不应该在方法声明中使用include。这是一个不好的做法。首先将MySQLi对象作为构造函数中的参数传递:

include_once("connection.php");
$place = new Places($mysqli) {
    $this->mysqli= $mysqli;
}

然后你会在你的班级中使用它;

$this->mysqli->real_escape_string

而不是

$mysqli->real_escape_string

答案 1 :(得分:1)

快速而肮脏的解决方案是使用数据库访问在方法中添加全局$ mysqli。像这样     

    include_once("connection.php");
    global $mysqli;
        $sSql = "INSERT INTO tblPlaces
            (Name, 
            Street, 
            HouseNumber, 
            City, 
            Category) 
            VALUES 
            ('" . $mysqli -> real_escape_string($this -> m_sName) . "', 
            '" . $mysqli -> real_escape_string($this -> m_sStreet) . "', 
            '" . $mysqli -> real_escape_string($this -> m_sHouseNumber) . "', 
            '" . $mysqli -> real_escape_string($this -> m_sCity) . "', 
            '" . $mysqli -> real_escape_string($this -> m_sCategory) . "')";


    if (!$mysqli -> query($sSql))
    {
        throw new Exception("Er is iets mis gelopen bij het toevoegen van een plaats");
    }

}
?>

肮脏的,我的意思是 真的 脏! - 但是如果要做到这一点,你制作它的方式将需要进行相当大的重组。

答案 2 :(得分:1)

常见的解决方案是在文件中包含实际使用这些连接的连接定义(换句话说,在高级代码中) - 而不是在库中,其中定义了一些与DB相关的函数和类。

文件本身通常是singleton类,构造函数和__clone方法都是私有的。像这样:

class DbConnection {
  private static $connection;
  private function __construct() {} // not needed in this example, really, as we only need a connection, which is a stable resource.
  private function __clone() {} // nothing to see here, move on!
  private function __wakeup() {} // ...and I really mean it!
  public static function getConnection() { 
    if (!isset(self::$connection) { 
      self::$connection = new mysqli(...);
      // and so created was a connection for all creatures, big and small, 
      // to share and enjoy!
    }
    return self::$connection; 
  }
}

然后,您可以在每次需要时使用...

安全地使用连接
  $conn = DbConnection::getConnection();

...包括该类文件只是ONCE。

答案 3 :(得分:0)

raina的答案中有一个小错误,这阻止了正确的脚本执行:

if (!isset(self::$connection)

应该是

if (!isset(self::$connection))