首页 > 解决方案 > 在插入数据之前检查带有条件的特定列的 SUM

问题描述

我需要帮助!我只想从用户插入数据,但在此之前它会首先检查数据库中的“dami”列是否<=100,如果是,那么它将插入用户输入的数据,如果不是,它将提示用户。

但是使用我的代码,无论“dami”总和如何,它都会不断插入。非常需要它!这是我的代码!

public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
    {
        $sql = "SELECT (SELECT SUM(dami)
                FROM tbl_business_card_information
                WHERE is_deleted = 1)
                AS 'Total'";

        if ('Total' >= 100){
        echo "Sorry but you already reach you maximum request";

        }else{

        $sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";

        $data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);                           
        $query = $this->db->query($sql,$data);
        return $query;  
        }
    }

谢谢你 !

标签: php

解决方案


你的用法SELECT是错误的。这是方法。

public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
{
    $sql = "SELECT SUM(dami) FROM tbl_business_card_information WHERE is_deleted = 1";
    $result = $this->db->query($sql);
    $row = $result->fetch_array(MYSQLI_NUM);
    $total = $row[0];
    if ($total > 100){
        echo "Sorry but you already reach you maximum request";
    }else{
        $sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";

        $data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);                           
        $query = $this->db->query($sql,$data);
        return $query;  
    }
}

推荐阅读