php - 在 php 中插入多个查询的多个条件

标签 php mysql mysqli

我需要在3个不同的条件下将数据插入到3个表中,例如table-01table-02table-03。 if condition 1 = true then insert into table-01, if condition 1 = true then insert into table-01 >,如果条件 1 = true 则插入到 table-01 & 如果所有 3 个条件都为 true,则插入到所有 3 个表中。

condition-01condition-02condition-03 来自 3 个输入值

var1 > 0
var2 > 0
var3 > 0
var4 > 0

我的尝试:

if(var1 > 0){
    $res = "INSERT INTO table1 (col1, col2, col3) VALUES ('1', '2', '3')";
}
if(var2 > 0){
    $res = "INSERT INTO table1 (col1, col2, col3) VALUES ('1', '2', '3')";
}
if(var3 > 0){
    $res = "INSERT INTO table1 (col1, col2, col3) VALUES ('1', '2', '3')";
}

需要帮助以正确的方式插入...

最佳答案

您应用的条件应该是降序的,例如

$x = 5; // let say 

if($x > 1){
     // your code runs in this block 
}
if($x > 2){
    // this condition is also true   
}
// also you have not used else if so multiple conditions will be true 

因此,根据您的情况,您应该使用

if($x > 3  && $x > 2 && $x > 1){
      // condition if all condition are true
}else if($x>3){
    // condition 1 
}else if($x > 2){
    // condition 2
} // so on .....

关于php - 在 php 中插入多个查询的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48534921/

相关文章:

php - Mysql- 查询项目仅在层次结构中达到第 2 级

php - 我需要坚持准备好的陈述吗?

php - mysqli_fetch_array 只显示第一个结果

php - json_encode 不会转义换行符

php - 创建相互依赖的对象

php - 什么是好的 PHP Active Record 库?

php - 如何使用php for循环将多个值插入数据库

java - Spring Boot 和 MySQL 的连接已关闭

mysql - MariaDB 不能在 docker 容器内吗

php - 将字符串常量与自动递增整数连接并存储在 mysql 数据库中