【问题标题】:If SELECT COUNT is less than or equal to 10, input reservation details如果 SELECT COUNT 小于或等于 10,则输入预订详情
【发布时间】:2014-04-17 13:57:20
【问题描述】:

我正在尝试用 PHP 制作一个简单的预订表格。

如果相同日期和时间的预订数量小于等于10,我希望它输入详细信息到表格中。

还有什么,我想要一条消息说“预订失败!我们当时已经订满了。”

问题:我在一个特定日期和时间以及测试时刻有 10 个预订,但是我的所有消息都返回为“预订失败!我们当时已订满。”无论何时。也没有导入任何内容。

我已经在没有 SELECT COUNT 查询的情况下测试了输入过程,并且它成功输入,所以我知道我的“小于或等于”查询有问题。

非常感谢任何帮助!谢谢

<?php 

require_once('lib/php/functions.php'); 
require_once('lib/php/db_connect.php');

//Grab Form Data
$reservationName = trim($_POST['reservationName']);
$reservationEmail = trim($_POST['reservationEmail']);   
$reservationNumber = trim($_POST['reservationNumber']);
$reservationDate = trim($_POST['reservationDate']);   
$reservationTime = trim($_POST['reservationTime']);   

$submit = trim($_POST['submit']);

$message = '';

if (!$db_server){ 
    die("Unable to connect to MySQL: " . mysqli_connect_error()); 
    $db_status = "not connected"; 
}else{

    //If submit comment pressed, get data and input 
    if (isset($submit) && $submit == 'Submit reservation'){    

        if ($reservationName&&$reservationEmail&&$reservationNumber&&$reservationDate&&$reservationTime){

            $reservationName = clean_string($db_server, $reservationName);
            $reservationEmail = clean_string($db_server, $reservationEmail);
            $reservationNumber = clean_string($db_server, $reservationNumber);
            $reservationDate = clean_string($db_server, $reservationDate);    
            $reservationTime = clean_string($db_server, $reservationTime);
            mysqli_select_db($db_server, $db_database); 

            $query="SELECT COUNT(*) FROM reservations AS reservationCount WHERE reservationDate='$reservationDate' AND reservationTime='$reservationTime'"; 
            $result=mysqli_query($db_server, $query); 

            $row=mysqli_fetch_array($result);

            if ($row <= 10){

                $query = "INSERT INTO reservations (reservationName, reservationEmail, reservationNumber, reservationDate, reservationTime)
                          VALUES ('$reservationName', '$reservationEmail', '$reservationNumber', '$reservationDate', '$reservationTime')"; 
                mysqli_select_db($db_server, $db_database); 
                mysqli_query($db_server, $query) or 
                        die("Insert failed: " . mysqli_error($db_server)) . $query; 

                $message = "<p class='success'>Booking successful! See you soon.</p>";

            }else{ 

                $message = "<p class='fail'>Booking failure! We're fully booked at that time.</p>";
            }              

        }else{
            $message = "<p class='fail'>Booking failure! Please fill in all the fields.</p>";  
        }    
    }
}             
?>

【问题讨论】:

  • @YourCommonSense 哦,别说了,伙计,我是一名学生,我的学习过程正在进行中。
  • 你有两个问题。 1)查询错误,2)结果检查错误。检查我的答案。
  • @LatheesanKanes 感谢 Latheesan 这已经解决了问题
  • 学习什么的都可以。只有堆栈溢出不是你的老师。这个网站是为了回答问题而不是向学生提问。
  • @YourCommonSense “这个网站是为了回答问题”。正确,这是一个问题。

标签: php sql select mysqli count


【解决方案1】:

首先,您的查询是错误的。将其更新为:

$query="SELECT COUNT(*) AS reservationCount FROM reservations WHERE reservationDate='$reservationDate' AND reservationTime='$reservationTime'"; 

查询错误的原因是,如果在表名后添加as reservationCount,它会变成表别名,而不是结果字段别名。

然后像这样更新执行检查的代码:

if ($row['reservationCount'] <= 10){

附:您的代码看起来对 SQL 注入开放。研究以面向对象的方式使用 MySQLi 库并绑定参数以防止 sql 注入。

看看这个例子,它涵盖了两个建议:http://php.net/manual/en/mysqli.prepare.php#refsect1-mysqli.prepare-examples

【讨论】:

  • 非常感谢,一切正常。我会尽快回答它!
【解决方案2】:

COUNT(*) 在一行中返回一个值,并且您正在检查行数,因此它将始终为 true,因为 1 &lt;= 10 始终为 true。将其更改为:

if ($row[0] <= 10){

【讨论】:

  • 感谢拉西尔的评论
  • 不客气。如果您想按名称检索值,Latheesan Kanes 的回答很好,但我个人更喜欢在只需要一个值时使用索引。这使您可以缩短查询(您可以删除AS reservationCount,因为它不再需要)并通过将$row[0] 复制到其他地方来重用您的代码,而不必每次都更改括号之间的名称。作为奖励,按索引检索值要快一些,尽管这对于一个值来说可以忽略不计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多