weixin_33721344 2016-01-03 22:59 采纳率: 0%
浏览 125

ajax请求的状态为500

part of my script:

.........................
.........................
data = $(this).serialize() + "&" + $.param(data);

                $.ajax({
                  type: "POST",
                  dataType: "json",
                  url: "fetch_tutor.php", //Relative or absolute path to response.php file
                  data: data,
                  success: function(data) {

                   }
                  });
......................
.........................

I spotted a weird thing. In the php script that I'm fetching the data from (fetch_tutor.php), I have a query that goes like this:

WHERE posts.subname LIKE '%tamil%' 
AND posts.pricing <= '161' 
AND posts.morning IN ('sun') 
OR posts.afternoon IN ('sun') OR posts.evening IN ('sun','mon') 

WHen I had this query I've no issue in fetching data from the php script but obviously '()' is missed arounf the IN.

So I replaced the above query with this (just added '()' brackets):

WHERE posts.subname LIKE '%tamil%' 
AND posts.pricing <= '161' 
AND (posts.morning IN ('sun') 
    OR posts.afternoon IN ('sun') 
    OR posts.evening IN ('sun','mon')) 

But now ajax request not fetching any data, in fact it shows status 500 (internal server error) in the console tab. Why is it so?

FULL PHP SCRIPT

<?php
session_start();
include('config.php');

$return = $_POST;

//sample data
//$return = '{"subject":"tamil","try":"","location":"kajang","lat":"","lon":"","rate":"100","distance":"40","avail":["Sun-1","Sun-2","Sun-3"],"action":"test"}';


  $return["json"] = json_encode($return);

  $data = json_decode($return["json"], true);

    $personal = $data['pers'];
    $tuition = $data['tuit'];
    $avail = $data['avail'];

    $days = array();
    $cols = array();

  $array = array();
  if (strpos($data['subject'], '>') != FALSE)
  {
  $array = explode(' > ', $data['subject']);
  $array[0]; // Languages
  $array[1]; // English
  $subject = $array[1];
  $catname = $array[0];
  }
  else
  {
  $subject = $data['subject'];
  }
  //after the symbol
   $catid = $data['try'];
  $location = $data['location'];
  //$lat = $data['lat'];
  //$lon = $data['lon'];
  $o_rate=$data['rate'];
  $rate = $data['rate']* 1.15;
  $distance =$data['distance'];
 //$address = '43000 kajang';
    $coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($location) . '&sensor=true');
    $coordinates = json_decode($coordinates);

     $coordinates->results[0]->geometry->location->lat;

     $coordinates->results[0]->geometry->location->lng;

    $lat = $coordinates->results[0]->geometry->location->lat;
    $lng = $coordinates->results[0]->geometry->location->lng;

 $lat = $lat;
 $lon = $lng;


/*insert starts*/
$insert="INSERT INTO search_history(place,budget,subject,level,datetime)VALUES('$location','$o_rate','$subject','$catname', now())";
        $stmt_insert =connection::$pdo->prepare($insert);
        $stmt_insert->execute();
/*insert ends*/

if($personal != "")
{
    $add_here = " AND centre =0";
}
if($tuition != "")
{
    $add_here .= " || centre =1";
}

$size = sizeof($avail);
if(($avail != "")&&($size > 1)){
$periods = array();
foreach($avail as $v){
    list($day, $column) = explode("-", $v); // make sure you validated the data to avoid errors
   $periods[$column][] = "'" . mysql_escape_string(strtolower($day)) . "'"; // PHP would automatically create an empty array if $periods[$column] was not defined
}
$intToCol = array(1 => "morning", 2 => "afternoon", 3 => "evening");
// $periods should now be identical to ["2" => ["'sun'", "'mon'"], "3" => ["'sun'"]]

$conditions = array();
foreach($periods as $int => $days){
    $dayString = implode(",", $days);
    $conditions[] = "posts." . $intToCol[$int] . " IN ($dayString)";
}
$add_here = implode(" OR ", $conditions) . "";
}else if(($avail != "")&&($size == 1))
{
    foreach($avail as $k=>$v)
        {
             $v;

            $array = explode('-', $v);
            $day =$array[0]; // Wed
            $column =  $array[1]; // 2

            if($column == 1)
            {
            $col = "morning";

            }
            if($column == 2)
            {
                $col = "afternoon";
            }
            if($column == 3)
            {
                $col = "evening";
            }

        }

    $add_here = "AND posts.".$col." = '".$day."' ";
}


if (strpos($data['subject'], '>') != FALSE)
  {
  $sql=" SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) *  pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance  
from posts,subjects WHERE  posts.catID = '$catid' AND posts.subname LIKE '%$subject%' AND posts.subid = subjects.subid AND posts.catID = subjects.catid  AND posts.pricing <= '$rate' AND (".$add_here.") GROUP BY posts.UUID having  distance <= '$distance' order by distance";
  }else
  {

 $sql = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) *  pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance  from posts WHERE posts.subname LIKE '%$subject%' AND posts.pricing <= '$rate' AND (".$add_here.") GROUP BY posts.UUID having  distance <= '$distance' order by distance";
  }


        $stmt =connection::$pdo->prepare($sql);
        $stmt->execute();
        $place=array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
               $place[] = $row;
               }
               $_SESSION['subject'] = $place;

echo json_encode($place);
  ?>

展开全部

  • 写回答

1条回答 默认 最新

  • weixin_33735077 2016-01-04 02:48
    关注

    I added the brackets here instead of in the query directly and the problem solved:

    $add_here = "AND (" . implode(" OR ", $conditions) . ")";
    
    评论
    编辑
    预览

    报告相同问题?

    悬赏问题

    • ¥15 PADS Logic 原理图
    • ¥15 PADS Logic 图标
    • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
    • ¥20 气象站点数据求取中~
    • ¥15 如何获取APP内弹出的网址链接
    • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部