我需要在3个不同的条件下将数据插入到3个表中,例如table-01
、table-02
、table-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-01
、condition-02
、condition-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/