3 回答

TA貢獻(xiàn)1802條經(jīng)驗 獲得超10個贊
WHERE
implode()
AND
OR
$wheres = array();$params = array();if (!empty($_GET['id'])) { $wheres[] = 'a.uid = :uid'; $params[':uid'] = $_GET['id'];}if (!empty($_GET['major'])) { $wheres[] = 'a.major = :major'; $params[':major'] = $_GET['major'];}if (!empty($_GET['name'])) { $wheres[] = 'b.name LIKE :name'; $params[':name'] = '%'.$_GET['name'].'%';}// And so on for all parameters$sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id";if (!empty($wheres)) { $sql .= " WHERE " . implode(' AND ', $wheres);}$stmt = $db->prepare($sql);$stmt->execute($params);
while ($student = $stmt->fetch()) { ...}

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊
<form action="<?= $_SERVER["REQUEST_URI"]; ?>" method="GET"> <input name="major" value="<?= $_GET["major"]; ?>" /> <select name="college"> <option value="1" <?PHP if( $_GET["college"] == 1 ) echo 'selected="true"'; ?>>Business</option> </select></form><?PHPif( ! empty( $_GET ) ){ if (isset($_GET['major'])) { $wheres[] = 'a.major = :major'; $params[':major'] = $_GET['major']; } if (isset($_GET['name'])) { $wheres[] = 'b.name LIKE :name'; $params[':name'] = '%'.$_GET['name'].'%'; } // And so on for all parameters $sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id"; if (!empty($wheres)) { $sql .= " WHERE " . implode(' AND ', $wheres); } $stmt = $db->prepare($sql); $stmt->execute($params);}?>
- 3 回答
- 0 關(guān)注
- 532 瀏覽
添加回答
舉報