JQueryAJAX触发的PHP比较未返回值


PHP compare triggered from JQuery AJAX not returning value

我使用jQuery调用PHP脚本来更新MYSQL数据库,但首先要根据该值进行逻辑测试。这个问题我发现了,如果我以这种方式构建查询,我会得到错误Query was empty

从逻辑上讲,我试图解决这个问题,但没有成功。如有任何帮助,我们将不胜感激。

$type= trim($_POST['type']);
if( $type == "wanted" ) {
   $qUpdate = "UPDATE Post...
}

POST可能无法通过正常的ajax 获得

$type= trim($_REQUEST['type']);
if( $type == "wanted" ) {
   $qUpdate = "UPDATE Post...";
   // Query must be correct in syntax ,I think its intentional for demo purpose
   if(mysql_query($qUpdate)) {
     echo "success";
   }
   else {
     echo "fail";
   }
}

这些"成功"answers"失败"文本将在您的ajax 上得到响应

try$_GET['type'],php脚本是从url调用的,而不是从表单调用的。所以它是$_GET参数,而不是$_POST

您应该检查您的查询,确保它都是正确的,并且变量被剥离,尝试这个

$type = mysql_escape_real_string($_POST['type']);
$sql = "UPDATE table....."
//query the code and ensure if there's an error kill the script
$res = mysql_query($sql) or die(mysql_error());

让我知道你过得怎么样!:)

解决方案似乎是我的比较不是比较苹果:

if ($type == mysql_real_escape_string("wanted") )