这是该项目的示例表:
TBL_测试:
id -
reference -
reciever -
created-
这是添加类并包含所有必需的文件(例如 db_con 和类初始化)并创建新对象后的 View 。
$refone = $_POST['refone'];
$reftow = $_POST['reftow'];
$ref3= $documentout->get_ref($refone,$reftow);
foreach($ref3 as $ref):
echo "this is test ".$ref['reference']."--------" .$ref['reciever']. "<br />";
endforeach;
<form method="POST" action="">
<lable>select_ref1</lable>
<select name="refone">
<option value=''>all</option>
<option value='1'>one</option>
<option value='2'>tow</option>
</select>
<lable>select_ref2</lable>
<select name="reftow">
<option value=''>all</option>
<option value='1'>one</option>
<option value='2'>tow</option>
</select></form>
这是回显结果的函数:
public function get_ref($refone, $reftow) {
$query = 'SELECT * FROM '. self::$table_name . ' WHERE reference = :refone AND reciever = :reftow';
$sth = $this->conn->prepare($query);
$sth->bindParam('refone', $refone, PDO::PARAM_INT);
$sth->bindParam('reftow', $reftow, PDO::PARAM_INT);
$sth->execute();
return $row = $sth -> fetchAll();
}
现在她是 idia,我想回显 reference = %whatever% 和 reciever=%whatever% 表示选择中的全部或两者都选择,因此它应该结果全部。
最佳答案
根据需要做一些逻辑来进行查询,一种解决方案可以是使用 ifs 来配置它(就像之前的两个答案一样)。我向您推荐的是使用 PDO 来创建查询。您能看到 http://php.net/manual/en/pdostatement.bindparam.php
最终查询字符串示例并执行它:
$query = 'SELECT * FROM table WHERE ref1 = :ref1 AND ref2 = :ref2';
$sth = $db->prepare($query);
$sth->bindParam('ref1', $ref1, PDO::PARAM_INT);
$sth->bindParam('ref2', $ref2, PDO::PARAM_INT);
sth->execute();
关于php - 如何在 select 中创建一个选项值以查找任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117973/