gpt4 book ai didi

php - 在 if else 语句中从不同查询中选择一个数据

转载 作者:行者123 更新时间:2023-11-30 00:48:01 26 4
gpt4 key购买 nike

从 test.php,我有一个 radio 输入。不同的值有不同的查询。当用户选择单选按钮时,我需要运行查询。因此选择具有所选期间(月份、上个月)的数据。我被困住了!如何从所有正在运行的查询中调出数据? :(

<tr> 
<td><label for="period"><b>Transaction Period:</b> </label></td>
<td><input type="radio" name="period" value="current">Current Month<br>
<input type="radio" name="period" value="current_first">Last 1 Month and Current Month<br>
<input type="radio" name="period" value="current_second">Last 2 Months and Current Month</td>
</tr>

$period = $_POST['period'];

if ($period == 'current'){
$query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND MONTH(CURDATE())= MONTH(datetime)";
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));

elseif ($period = 'current_first'){
$query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND datetime BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE";
$result2 = mysqli_query($link, $query2) or die(mysqli_error($link));
}

else if ($period ='current_second'){
$query3 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND datetime BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
}

else {
}

while ($row1 = mysqli_fetch_array($result1/$result2/$result3)) {
$date = $row1['datetime'];
$trans_code = $row1['transaction_code'];
$reference = $row1['reference'];
$debit = $row1['debit'];
$credit = $row1['credit'];


echo $date;
echo $trans_code;
echo $reference;
echo $debit;
echo $credit;

最佳答案

第一个 if 语句后缺少一个结束花括号}

而且,为什么不按如下方式更改 if ... else ...while .. 语句并尝试。

if ($period == 'current') {
$query = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND MONTH(CURDATE())= MONTH(datetime)";
} elseif ($period = 'current_first') {
$query = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND datetime BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE";
} else if ($period ='current_second') {
$query = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND datetime BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE";
} else {
// what should i do?
}

$result = mysqli_query( $link, $query ) or die( mysqli_error( $link ) );
while ( $row = mysqli_fetch_array( $result ) ) {
// do something
$date = $row[ 'datetime' ];
...

echo $date;
...
} // while

关于php - 在 if else 语句中从不同查询中选择一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21209128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com