ajax请求的状态500

时间:2016-01-04 06:59:53

标签: php jquery ajax

我的剧本的一部分:

.........................
.........................
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) {

                   }
                  });
......................
.........................
我发现了一件奇怪的事。在我从(fetch_tutor.php)获取数据的php脚本中,我有一个类似这样的查询:

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') 

当我收到此查询时,我从php脚本中获取数据时没有任何问题,但显然'()'错过了IN

所以我用这个替换了上面的查询(只添加了'()'括号):

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')) 

但是现在ajax请求没有获取任何数据,实际上它在控制台选项卡中显示状态500(内部服务器错误)。为什么会这样?

  

完整的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 个答案:

答案 0 :(得分:0)

我在这里添加了括号而不是直接在查询中解决了问题:

$add_here = "AND (" . implode(" OR ", $conditions) . ")";