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

时间:2014-01-18 19:56:55

标签: php mysql

从test.php,我有一个无线电输入。 不同的值具有不同的查询。 当用户选择单选按钮时,我需要运行查询。 因此,选择具有所选时段(月,上个月)的数据。 我卡住了!如何从所有运行的查询中调出数据? :(

<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;

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题;您希望在用户选择单选按钮时提交表单吗?如果是这样,您必须使用表单包围输入类型,并将onClick="this.form.submit();"添加到输入类型。所以:

<form method="post" action="">
<tr> 
<td><label for="period"><b>Transaction Period:</b> </label></td>
<td><input type="radio" name="period" value="current" onclick="this.form.submit();">Current Month<br>
<input type="radio" name="period" value="current_first" onclick="this.form.submit();">Last 1 Month and Current Month<br>
<input type="radio" name="period" value="current_second" onclick="this.form.submit();">Last 2 Months and Current Month</td>          
</tr>
</form>

哦,顺便说一下,在执行if和查询代码之前,还应该检查是否设置了$_POST['period']

答案 1 :(得分:0)

在第一次}声明后,有一个结束花括号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