php - 在插入数据之前检查带有条件的特定列的 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;
}
}
谢谢你 !
解决方案
你的用法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;
}
}
推荐阅读
- android - Firebase 安全规则 UserPhoneId
- bixby - 如何在没有用户的情况下让 Bixby 询问输入
- r - R如何将数据重塑为列变量以成为行名并提取特定的其他变量数据?
- javascript - 多个 V 模型 Javascript Vue Js
- oracle - 如何优化 PL/SQL 中的嵌套 CASE 语句
- java - 适用于 Android 的平台签名 OTA 更新程序应用程序
- python-3.x - 如何在 Python 3 的数据框中为不同的条件设置参数
- vim - Vim:跳转系统verilog模块的开头还是结尾?
- reactjs - npm run build 后未找到清单和其他几个静态文件(字体)
- javascript - React onClick 方法在第一次选择时给出 undefined 和传递的值不一样?