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