[英]JSON/PHP not working when using mysql queries
我正在嘗試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);
}
?>
我看不到您正在建立連接$ con的任何地方。 該連接建立與數據庫的連接。 一旦有了它,就可以查詢它。
您應該具有以下內容(顯然已替換了正確的值):
$con = mysqli_connect("myhost","myuser","mypassw","mydb") or die("Error " . mysqli_error($link));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.