MySQL查询不适用于PHP

MySQL查询不适用于PHP,php,mysql,Php,Mysql,我有一个PHP代码,其中输入的字段值应该与数据库表中的值匹配。若匹配项找到,它必须做一些工作,否则它必须说“未找到匹配项” 但每当我检查表(数据库)中不存在的值时,它就会显示“已找到匹配项” 代码如下: <?php include"conn.php"; if($_SERVER['REQUEST_METHOD'] == "POST"){ // Get data // Vehicle Number $vehicle_numb

我有一个PHP代码,其中输入的字段值应该与数据库表中的值匹配。若匹配项找到,它必须做一些工作,否则它必须说“未找到匹配项”

但每当我检查表(数据库)中不存在的值时,它就会显示“已找到匹配项”

代码如下:

<?php

      include"conn.php";

      if($_SERVER['REQUEST_METHOD'] == "POST"){

    // Get data   
    // Vehicle Number
         $vehicle_number = isset($_POST['VehicleNumber']) ?mysql_real_escape_string($_POST['VehicleNumber']) : "";       
    //ID card Nos.
         $idcardno1= isset($_POST['idno1']) ? mysql_real_escape_string($_POST['idno1']) : "";
    $idcardno2= isset($_POST['idno2']) ? mysql_real_escape_string($_POST['idno2']) : "";

    //Text Messages
         $Textmsg = isset($_POST['yourtext']) ? mysql_real_escape_string($_POST['yourtext']) : "";

      if($idcardno1 == NULL )
        {
      //echo "Blank Fields";
          $json = array("status" => "Failure", "msg" => "User has entered one or more than one null values so couldn't go for database operations");    
        }
    else
        {

         $q=mysql_query("SELECT * FROM profile WHERE IDcardno1 ='$idcardno1' OR IDcardno2 = '$idcardno2' OR VehicleNumber ='$vehicle_number'" )or die(mysql_error());
          if($q)
        {
        $num=mysql_num_rows($q);
        if($num==0)
           {
          $json = array("status" => "Failure", "msg" => "Information not found");
          print $num;
        }

          else{
          $json = array("status" => "success", "msg" => "Information is stored and match has been found");
         }
       }
     }
   }

  else{
    $json = array("status" => "Failure", "msg" => "POST_Request method not    accepted");
  }

  mysql_close($con);

  /* Output header */
  //header('Content-Type: application/json');
  echo json_encode($json);

?>  

因为您正在使用SQL查询或在SQL查询中使用SQL查询,所以它将返回存在空数据库值和空表单值的任何数据库行。这可能会返回您不希望返回的行(取决于您的意图)

如果是这种情况,您可能希望仅匹配通过将查询更改为以下内容而实际填充的字段值:

$q = mysql_query("SELECT * FROM profile WHERE (IDcardno1 ='$idcardno1' AND '$idcardno1' <> '') OR (IDcardno2 = '$idcardno2' AND '$idcardno2' <> '') OR (VehicleNumber ='$vehicle_number'  AND '$vehicle_number' <> '')" )or die(mysql_error());
$q=mysql\u查询(“从配置文件中选择*(IDcardno1='$IDcardno1'和'$IDcardno1'')或(IDcardno2='$IDcardno2'和'$IDcardno2'')或(车辆编号='$vehicle\u number'和'$vehicle\u number'')或死亡(mysql\u错误();

(这只是一种方法)

您应该重新格式化代码。它完全不可读。@Jan.J但下面两个通信的人是如何阅读代码的?您确实意识到,当您检查三元语句中是否设置了变量时,实际上是将变量设置为空(“”),而不是空的。此外,要检查变量是否为null,请尝试改用is_null。在您的情况下,应该使用empty(),因为如果变量为空字符串、false、null或0为“不相等”,它将返回true。因此,此查询只检查不是EmptyTanks@Manachi的字段值。它起作用了!!