在插入数据之前,先检查特定列的SUM是否符合条件

时间:2018-07-25 03:32:43

标签: php

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

但是使用我的代码,无论'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;  
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

您对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;  
    }
}