doukuang6795 2015-08-09 02:53
浏览 37
已采纳

PHP如果SQL计数的条件语句

I am working on a form, in which it is required that I check if there are 1 or more instances of a particular value/ values. The problem is that on testing, I get "Dates are already booked", even though the values I select are not.

This is my code -

if ($_POST) {
   if (isset($_POST['proceedtopaypal'])){
    $apartment = $_POST['apartment'];
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $mobile = $_POST['mobile'];
    $pax = $_POST['pax'];
    $address = $_POST['address'];
    $remarks = $_POST['remarks'];
    $day_from = $_POST['day_from'];
    $month_from = $_POST['month_from'];
    $year_from = $_POST['year_from'];
    $booking_from = $year_from."-".$month_from."-".$day_from; (format- yyyy-mm-dd)
    $day_to = $_POST['day_to'];
    $month_to = $_POST['month_to'];
    $year_to = $_POST['year_to'];
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from));     
    $days = floor($no_of_nights / (60*60*24));
    $validdate = false;
    $buttonpressed = false;

            function IsInjected($str)
    {
      $injections = array('(
+)',
                  '(+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }

if (!checkdate($month_from, $day_from, $year_from)) {
        echo "Check in date is invalid";
    }
    else if (!checkdate($month_to, $day_to, $year_to)) {
        echo "Check out date is invalid";
    }
    else if ($booking_from > $booking_to) {
             echo "Check in date is after check out date";
    }
    // check if all info is filled in 
    else if (($name == "Name") || ($surname == "surname") || ($email == "Email") || ($mobile == "mobile") || ($address == "Address")) {
        echo "Please fill in the missing information";
    }
    else if (IsInjected($email)) {
        echo "Not an email";
    } 
    else if ($validdate == false) {
        $final = true;
        include 'connect.php';
        $sql = "SELECT COUNT(*)  FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates < '$booking_to'";
        $result = mysqli_query($conn, $sql);
        $data = mysqli_num_rows($result);
        $row=mysqli_fetch_row($result);
        echo $row[0];   

        if ($data > 0) {
            echo "Dates are already booked!";
        }
        else {
            echo "Proceed";
        }

        }

    }
}

What would be the best solution for this problem so that I can proceed with my works.

  • 写回答

1条回答 默认 最新

  • dongxun2903 2015-08-09 04:19
    关注

    The $data = mysqli_num_rows($result); is useless. When you use SELECT COUNT(*) in your query, you should not use mysqli_num_rows function. You can use just this:

    $sql = "SELECT COUNT(*)  FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates < '$booking_to'";
        $result = mysqli_query($conn, $sql);
        $row=mysqli_fetch_row($result);
        echo $row[0];   
    
        if ($row[0] > 0) {
            echo "Dates are already booked!";
        }
        else {
            echo "Proceed";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路