我有一个搜索表单:
<input type="text" name="srchby" />
<input type radio name="crit" value="a" />
<input type radio name="crit" value="b" />
<input type="submit" value="proceed" name="proceed"/>
我有一个存储并提供搜索条件的数组,用户是选择无线电“a”还是“b”。
$crit=mysql_real_escape_string($_POST["crit"]);
$srchby=mysql_real_escape_string($_POST["srchby"]);
$crar=array("a"=>array("column_name1","column_name2","table_name"),
"b"=>array("column_name1","column_name2","table_name2"));
mysql_query("SELECT '$crar[$crit][0]','$crar[$crit][1]'
FROM '$crar[$crit][3]' WHERE something='$srchby'");
这是一个好的&amp;快速选择与否?你有更好的想法吗?
由于
答案 0 :(得分:1)
$crit=mysql_real....($_POST["crit"]);
$crar=array("table1"=>array("field1","field2","field3"),
"table2"=>array("field1","field2","field3"));
$fieldList='';
foreach($crar[$crit] as $fields)
{
$fieldList.=" $fields, "
}
rtrim($fieldList,", ");
$query="select $fieldList from $crit";
答案 1 :(得分:1)
我认为代码的更准确和干净的版本将是这样的:
$crit=mysql_real_escape_string($_POST["crit"]);
$srchby=mysql_real_escape_string($_POST["srchby"]);
switch($crit){
case "a":
mysql_query("SELECT `column_name1`, `column_name2`
FROM `table_name1` WHERE something='$srchby'");
break;
case "b":
mysql_query("SELECT `column_name1`, `column_name2`
FROM `table_name2` WHERE something='$srchby'");
break;
}