如何验证来自数据库的多个输入?

时间:2016-01-08 09:36:15

标签: php mysql

我不想创建验证,如果DATE,TIME和COTTAGE已经保留/可用,请检查数据库。如果其中一个中的任何一个不可用,它将保存到数据库中。

这是我制作的代码:



<?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;
}
?>
&#13;
&#13;
&#13;

但我有一个错误:

警告:mysqli_num_rows()期望参数1为mysqli_result,第22行的C:\ xampp \ htdocs \ reservation \ formup.php中给出布尔值

和第22行是

$getrow = mysqli_num_rows($qry);

但是当我使用这个多选查询时:

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

它将保存到数据库,但当小屋在数据库上有相同的小屋时,它不会保存......

请帮我解决这个问题....

2 个答案:

答案 0 :(得分:1)

通过msyql查询的文档:

  

失败时返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。

Link

据我了解,查询成功或不成功true / false返回mysqli_result对象。所以我认为你的查询没有返回值。尝试在db上运行sql查询并检查是否返回了预期的值?

答案 1 :(得分:0)

mysqli_num_rows()成功时返回true,失败时返回false; for select,show等mysqli_query返回结果对象。 我可能需要看到完整的代码才能更好地了解那里的错误但是试试这个:

&#13;
&#13;
$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";
}
&#13;
&#13;
&#13;