我正在尝试使用json,并且出于某种原因,这种方式可以正常工作,直到我实现任何类型的mysql。我从教程中偷了这个。我尝试使用预处理语句和基本的mysqli查询;但是,当我这样做时,不会返回任何数据。
这是jquery:
<script type="text/javascript">
$("document").ready(function(){
$(".sendText").submit(function(){
$(".errors").html("");
$(".success").html("");
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "jsonpost.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
if(data["success"]=="yes") {
$(".success").html("Message Sent!");
$(".formContainer").html("" + data["json"] + "");
} else {
$(".errors").html("" + data["errors"] + "");
}
}
});
return false;
});
});
</script>
这是php:
<?php
header('Content-Type: application/json');
require "../database/db.php";
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$return = $_POST; //to reference post
$phone=$return[phone];
$yphone=$return[yphone];
$alert=$return[alert];
function generateRandomString($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
$confirmCode=generateRandomString(6);
//validate sender phone number
if(empty($phone)){
$c="0"; $empty="<li>Please enter their phone number!</li>";
}
//see if content is 140 characters or less
if(strlen($content)>140){
$c="0"; $lerror="<li>Your message must be 140 characters or less in length!</li>";
}
/* IP Address Info */
$ip=$_SERVER["REMOTE_ADDR"]; //get sender IP address
$ip=hash('sha256', $ip); //hash IP address
/* END IP Info */
$etime=time()-18000; //5 hours ago from current time
$time=time(); //current time
/*This makes the code fail */
$result = mysqli_query($con,"SELECT * FROM messages WHERE ip='$ip' AND phone='$phone' AND time BETWEEN $etime AND $time"); //check if ip has sent more than 5 messages to phone number in less than 5 hrs.
$totaltexts=mysqli_num_rows($result);
if($totaltexts>4){
$c="0"; $exceedmessage="<li>You have sent $phone the limit of 5 messages per 5 hours. Please try again later.</li>";
}
/* ------------------------- */
if($c=="0"){
$return["success"]="no";
$return["errors"]="$empty $phoneerror $hooderror $yphoneerror $lerror $exceedmessage $exceedmessagea $exceedmessage2";
} else {
$return["success"]="yes";
}
$return["json"] = json_encode($return);
echo json_encode($return);
}
?>
答案 0 :(得分:0)
我没有看到你在设置连接$ con的任何地方。连接建立与数据库的连接。完成后,您可以查询它。
您应该具有以下内容(显然替换了正确的值):
$con = mysqli_connect("myhost","myuser","mypassw","mydb") or die("Error " . mysqli_error($link));