MySQL查询无法使用PHP

时间:2015-08-19 07:22:45

标签: php mysql

我在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);

?>  

1 个答案:

答案 0 :(得分:0)

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

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

<IfModule mod_ssl.c>
    <VirtualHost 92.168.0.10:443>
        DocumentRoot /var/www/html/alias.example.com
        ServerName some.site.info
        <Directory "/var/www/html/alias.example.com">
            allow from all
            Options None
        </Directory>
        SSLCertificateFile /var/www/certs/example.com.crt
        SSLCertificateKeyFile /var/www/certs/example.com.key.nopass
        SSLCACertificateFile /var/www/certs/intermediate.crt
        SSLEngine on
    </VirtualHost>
</IfModule>

(这只是一种方法)