0

我收到了这个错误,我真的不知道该怎么办。我很想构建一个“表单”,您可以使用它向数据库添加信息。因为我不想在我的数据库中出现双重记录,所以我正在使用 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

3 回答 3

2

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

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

然后你会像这样在你的课堂上使用它;

$this->mysqli->real_escape_string

代替

$mysqli->real_escape_string
于 2012-05-07T13:04:57.683 回答
2

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

文件本身通常是一个单例类,构造函数和 __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();

...仅包含该类文件一次。

于 2012-05-07T13:23:30.797 回答
0

一个快速而肮脏的解决方案是使用数据库访问在您的方法中添加全局 $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");
    }

}
?>

脏,我的意思是真的很脏!- 但是,如果要以其他方式制作,那么您制作它的方式将需要进行相当大的重组。

于 2012-05-07T13:03:40.160 回答