使用SUM()问题选择付款的简单单选按钮

时间:2014-07-30 05:22:57

标签: php mysql sql

这是我尝试使用单选按钮从MySQL数据库执行查询。我不确定我是否在正确的轨道上,因为我仍然在尝试测试代码,但是它给了我一个空白页面,甚至没有显示first_payment

我希望创建一个简单的代码,以便在输出页面上显示带有SUM()的单选按钮列表。我创建了一个数据库,我需要的是选择使用SUM()进行汇总的选项。

HTML代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
</head>

<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代码:

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

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

$req=$_REQUEST['payment'];

// mysql_select_db($database) or die( "Unable to select database");

if ($req="first") $query="SELECT * FROM `Customer Information` SUM('$first_payment')";

$result=mysqli_query($query);

while($row = mysqli_fetch_array($result)) {
echo $row['first_payment'];
}
mysqli_query($result);
mysqli_close();


?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

你混合了Mysql和mysqli。你必须使用一种方法。我修复了一些编码错误

<?php 
$con=mysqli_connect("localhost","xx","xx","xx");

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

$req=$_REQUEST['first_payment'];

if ($req="first") {
    $query="SELECT * FROM `Customer Information` SUM('$first_payment')";
}

$result=mysqli_query($query);

while($row = mysqli_fetch_array($result)) {
echo $row['first_payment'];
}
mysqli_query($result);
mysqli_close();


?>

答案 1 :(得分:0)

Radio Buttons允许您从一组值中选择一个值。

因此,如果您想拥有多个值,则需要使用check boxes

如果您尝试将单选按钮的值输入数据库,则需要将radio buttons更改为此

<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

现在,当用户选择支付组的值时,它将发布。然后,您可以接受该帖子并执行类似

的操作
$payment = $_POST[payment];

现在value的{​​{1}}是POSTvariable

我们可以将其输入$payment

database