dongniuxia8650 2014-07-30 09:11
浏览 20
已采纳

不在所选的特定单选按钮上显示其他结果

Using 4 radio button as option to select the payment column to SUM all the number up. I wrote if select is equals to first then run first query, and if it's second to run second query. It's displaying on the first payment even if I select second. Is it because of my echo at the end of the code? Can anyone help please? Should be using for?

HTML Coding (paycal.html):

<body>
<form name="input" action="paycal.php" method="post">
<fieldset>
<legend>Which payment do you want to SUM up:</legend>
    <input type="radio" name="payment" value="first" /> First 
    <input type="radio" name="payment" value="second" /> Second
    <input type="radio" name="payment" value="third"  /> Third
    <input type="radio" name="payment" value="fourth"  /> Fourth

</fieldset>
<input type="submit" value="Submit" />   
</form>
</body>
</html>

PHP Coding (paycal.php):

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


$pay=$_REQUEST['payment']; 

// MySQL database connection, username, password, database name
$con=mysqli_connect("localhost","username","password","database_name");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if ($pay=="first") $query="SELECT *, SUM(first_payment) FROM `Customer Information`";
else if ($pay=="second") $query="SELECT *, SUM(second_payment) FROM `Customer Information`";
else if ($pay=="third") $query="SELECT *, SUM(third_payment) FROM `Customer Information`";
else if ($pay=="fourth") $query="SELECT *, SUM(fourth_payment) FROM `Customer Information`";


$result = mysqli_query($query);

// Executing and error checking of query
if (!mysqli_query($con,$query)) {
  die('Error: ' . mysqli_error($con));
}

while($row = mysqli_fetch_array($result)) {

 echo $row['first_payment'];
}

// Close MySQL
mysqli_close($con);
?>
  • 写回答

2条回答 默认 最新

  • drm16022 2014-07-30 09:36
    关注
    while($row = mysqli_fetch_array($result)) {
        echo $row[$pay.'_payment'];
    }
    

    Simply make the key produced dynamic using the variable you use which is ($pay)

    In other thoughts, this is very bad table design. If you had a better design you wouldn't fall into such problems. I suggest you change it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?