1

I wan't to create a validation, check the database if the DATE, TIME and COTTAGE is already reserved/available. If any in one of the 3 is unavailable, it will save to the database.

Here is the code I made:

<?php
require_once("config.php");
require_once("includes/inputs.php");

 //fetching the data of the cottage
 $sql = "SELECT * FROM cottage WHERE id='$cottage'";
    $result = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($result);
    $name_cottage = $row["name"];

    //fetching the data of the time
    $sql = "SELECT * FROM rate WHERE id='$daytime'";
    $result = mysqli_query($link, $sql);
    $row = mysqli_fetch_array($result);
    $daytime_name = $row["name"];

  //validating whether the date, time and cottage is already available in the cottage
  $validate = "SELECT * FROM reserve WHERE date = '$date' AND daytime = '$daytime' AND cottage = '$cottage'";
  $qry = mysqli_query($link, $validate);
  $getrow = mysqli_num_rows($qry);

  if( $getrow > 0 ){
      echo " $date, $daytime_name and $name_cottage  has already been taken!";
      return false;
  }
  else{
 $sql = "INSERT into reserve (name, address, contact, email, date, time, ahc, chc, cottage, promo, total) values ('$name','$address','$contact', '$email', '$date', '$daytime', '$ahc', '$chc', '$cottage', '$promo', '$calc')";
 $result = mysqli_query($link,$sql);
 echo " $date, $daytime_name and $name_cottage is successfully reserved!";
 return true;
}
?>

But i am having an error of:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\reservation\formup.php on line 22

and line 22 is

$getrow = mysqli_num_rows($qry);

but when i use this multiple select query:

  $validate = "SELECT * FROM reserve WHERE date = '$date'";
  $validate = "SELECT * FROM reserve WHERE daytime = '$daytime'";
  $validate = "SELECT * FROM reserve WHERE cottage = '$cottage'";

it will save to the database but when the cottage have the same cottage on the database it won't save...

please help me to fix this....

Joyce
  • 17
  • 6

2 Answers2

1

By the documentation of msyql query:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Link

So as i understand it the query is successful or not succesful true / false is returned else a mysqli_result object. So i think your query have no return value. Tried running the sql query on the db and have you check that the expected values are returned?

Nyranith
  • 367
  • 2
  • 6
  • 15
0

mysqli_num_rows() returns true on success and false on failure; for select, show etc. mysqli_query return result object. I may need to see the full codes to get better idea whats wrong there but try this:

$qry = mysqli_query($link, "SELECT * FROM reserve WHERE date = '$date' AND daytime = '$daytime' AND cottage = '$cottage'");

$getrow = mysqli_num_rows($qry);

if( !$getrow == 0 ){
  echo ".....taken";
}
else{
  $sql = mysqli_query($link, "insert.....");
  echo "...reserved";
}
  • it still have this error Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\reservation\formup.php on line 19 – Joyce Jan 08 '16 at 10:30
  • could you show us the full code or you mail me in a zip file the pages and exported sample sql so i can test out and check for you where it went wrong! monirussalam@gmail.com – Monirus Salam Jan 08 '16 at 10:34
  • its working properly now sir thank you for the help :) – Joyce Jan 14 '16 at 12:08