根据我的radiobutton选择显示我的sql表结果

时间:2015-07-16 06:20:37

标签: php html mysql database radio-button

我有我的radiobuttons,

selection1:
field1<input type="radio" name="field1" value="field1"/>
field2<input type="radio" name="field2" value="field2"/>
field3<input type="radio" name="field3" value="field3"/>

selection2:
type1<input type="radio" name="type1" value="type1"/>
type2<input type="radio" name="type2" value="type2"/>
type3<input type="radio" name="type3" value="type3"/>

我在数据库表中有预定义的值&#34; test&#34;如

field1   type1  10
field1   type2  20
field1   type3  30
field2   type1  30
field2   type2  50
field2   type3  60
field3   type1  80
field3   type2  90
field3   type3  100

如果用户选择"field1""type1",则应将结果显示为10,如果用户选择"field2""type3"它应显示60,我该怎么办呢? php,任何想法,甚至是小想法都是受欢迎的。

2 个答案:

答案 0 :(得分:2)

首先,您必须为不同的单选按钮组使用不同的名称

接下来,如果您想在HTML表单标签中使用它,您可以执行以下操作

<form action='test.php' method='post'>
selection1:
field1<input type="radio" name="field" value="field1"/>
field2<input type="radio" name="field" value="field2"/>
field3<input type="radio" name="field" value="field3"/>

selection2:
type1<input type="radio" name="type" value="type1"/>
type2<input type="radio" name="type" value="type2"/>
type3<input type="radio" name="type" value="type3"/>
<input type='submit'>
</form>

在PHP代码中你应该使用像这样的函数

function get_value($type,$field)
 {
 $sql="select result from test where field='$field' and type='$type'";
 $query=mysql_query($sql) or die(mysql_error());
 $result=mysql_fetch_array($query);
 return $result[result];
}

$field=$_POST['field'];
$type=$_POST['type'];
echo get_value($type,$field);

这对你有用

答案 1 :(得分:0)

public Datatable GetRecord(string fieldName, string type)
{
  //Call Store Procedure and Pass fieldName , Type as param 
}

In you Store Procedure:

@FieldParam nvarchar(max),
@FieldType  nvarchar(max)
as 
begin
 select * from test where Field = @FieldParam and Type = @FieldType 
end