检查SQL PHP上的两列是否匹配

时间:2018-07-05 10:45:56

标签: php mysql

我有一个表格,用户必须输入他们的预订ID和姓氏。如果数据库中这两个值匹配,那么我需要从数据库中返回相应的值。

我有两个文件,一个是使用ajax的html文件,另一个是php文件。单击该按钮时,什么也没有返回,我没有看到任何特定的错误,并且我确定输入的值是正确的。

<script>
var ajax = getHTTPObject();

function getHTTPObject()
{
    var xmlhttp;
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      //alert("Your browser does not support XMLHTTP!");
    }
    return xmlhttp;
}

function updateCityState()
{
    if (ajax)
    {
        var reservation_id = document.getElementById("reservation_id").value;
          var guest_last_name = document.getElementById("guest_last_name").value;

        if(reservation_id)
        {

            var param = "?reservation_id=" + reservation_id + "&guest_last_name=" + guest_last_name;
var url = "test04.php";

            ajax.open("GET", url + param, true);
            ajax.onreadystatechange = handleAjax;
            ajax.send(null);

        }
    }
}
function handleAjax()                                                                                                                           
{
  if (ajax.readyState == 4)
   {
        var guest_full_name = document.getElementById('guest_full_name');

            var unit_number = document.getElementById('unit_number');

    var floor = document.getElementById('floor');

        var key_sa = document.getElementById('key_sa');




    if(!!ajax.responseText) {
      var result = JSON.parse(ajax.responseText);
      if(!!result){
         guest_full_name.innerHTML  = (!!result.guest_full_name) ? result.guest_full_name : '';

             unit_number.innerHTML = (!!result.unit_number) ? result.unit_number : '';


        floor.innerHTML = (!!result.floor) ? result.floor : '';


        key_sa.innerHTML = (!!result.key_sa) ? result.key_sa : '';
      } 
    }
   }
 }
</script>



<p id='employee_name'></p>
<p id='employee_age'></p>
<p id='safe_code'></p>

我的test04.php

<?php

$conn = mysqli_connect("","","","");

$reservation_id = mysqli_real_escape_string($conn, $_GET['reservation_id']);
$guest_last_name = mysqli_real_escape_string($conn, $_GET['guest_last_name']);

$query = "SELECT reservation_id, guest_full_name, guest_last_name unit_number, floor, key_sa FROM reservations2 INNER JOIN guest ON (reservations2.reservation_id=guest.reservation_idg) INNER JOIN unit USING (unit_id) where reservation_id ='".$reservation_id."'AND guest_last_name ='".$guest_last_name."";


 $result = mysqli_query($conn, $query) or die(mysql_error());
$response = array();

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {



        $response['guest_full_name'] = ($row['guest_full_name'] != '') ? $row['guest_full_name'] : '';
         $response['unit_number'] = ($row['unit_number'] != '') ? $row['unit_number'] : '';

             $response['floor'] = ($row['floor'] != '') ? $row['floor'] : '';
        $response['key_sa'] = ($row['key_sa'] != '') ? $row['key_sa'] : '';



    }
}
echo  json_encode($response, true); 


?>

1 个答案:

答案 0 :(得分:0)

  

我没有看到任何特定的错误

  • 您在哪里看?
  • 您是否检查了来自PHP脚本的原始响应,还是只是查看浏览器中呈现的内容?
  • 您是否已验证错误日志记录是否正常工作,是否检查了日志?

PHP的逻辑尚不清楚-JSON数据和PHP数组无法处理多个记录,但您可以处理多个记录。正确实现REST会很好。这也应该应用身份验证并使用CSRF进行安全保护-但我假设您出于说明目的而将其省略。

您的代码未编写为处理故障或丢失数据。考虑(注意与您发布的内容的所有差异):

<?php

$conn = mysqli_connect("","","","");
$response = array();

$reservation_id = mysqli_real_escape_string($conn, $_GET['reservation_id']);
$guest_last_name = mysqli_real_escape_string($conn, $_GET['guest_last_name']);

$query = "SELECT reservation_id, guest_full_name
  , guest_last_name unit_number, floor, key_sa 
  FROM reservations2 
  INNER JOIN guest 
  ON (reservations2.reservation_id=guest.reservation_idg) 
  INNER JOIN unit USING (unit_id) 
  WHERE reservation_id ='".$reservation_id."'
    AND guest_last_name ='".$guest_last_name."";
$result = mysqli_query($conn, $query);
if (!$result) {
      $response['status']=503
      $response['msg']="Error";
      trigger_error(mysql_error());
      finish($response);
      exit;
}

$response['status']=200;
$response['msg']='OK';
$response['guest_full_name'] = htmlentities($_GET['guest_last_name']);
$response['reservations']=array();

while($row = mysqli_fetch_assoc($result)) {
    $response['reservations'][]=array(
           'unit_number'=>$row['unit_number'],
           'floor'=>$row['floor'],
           'key_sa'=>$row['floor_sa']);
  }
}
finish($response);
exit;

function finish($response)
{
    header("HTTP/1.1 $response[status] $response[msg]");
    header("Content-type: application/json");
    echo  json_encode($response, true); 
}