我對PHP還是很新的,所以如果我忽略了一些簡單的東西,請原諒我。我正在嘗試構建一個會員搜索表單,通過輸入一個或多個標準來允許用戶查找成員:名字或用戶名,城市,州,國家或通過電子郵件地址。如果輸入單個字段,則表單可以工作,或者只有當名稱/用戶名字段具有值時,該表單纔有效。假設這是一個邏輯問題。提前致謝。PHP成員搜索的問題
if (!isset($_POST['fname']))
{
//If not isset -> set with dummy value
$_POST['fname'] = "undefine";
}
if (!isset($_POST['city']))
{
//If not isset -> set with dummy value
$_POST['city'] = "undefine";
}
if (!isset($_POST['state']))
{
//If not isset -> set with dummy value
$_POST['state'] = "undefine";
}
if (!isset($_POST['country']))
{
//If not isset -> set with dummy value
$_POST['country'] = "undefine";
}
if (!isset($_POST['email']))
{
//If not isset -> set with dummy value
$_POST['email'] = "undefine";
}
// DEFAULT QUERY STRING
$queryString = '';
if ($_POST['fname'] != '') {
$fname = $_POST['fname'];
$fname = stripslashes($fname);
$fname = strip_tags($fname);
$fname = preg_replace('#[^A-Za-z 0-9]#i', '', $fname);
$fname = mysql_real_escape_string($fname);
$queryString = "(firstname LIKE '%$fname%' OR username LIKE '%$fname%')";
} else {
$queryString = '';
}
if ($_POST['city'] != '') {
if (($_POST['fname'] != '') || ($_POST['state'] != '') || ($_POST['country'] != '') || ($_POST['email'] != '')){
$city = $_POST['city'];
$city = stripslashes($city);
$city = strip_tags($city);
$city = preg_replace('#[^A-Za-z 0-9]#i', '', $city);
$city = mysql_real_escape_string($city);
$queryString .= " AND city='$city'";
} else {
$city = $_POST['city'];
$city = $_POST['city'];
$city = stripslashes($city);
$city = strip_tags($city);
$city = preg_replace('#[^A-Za-z 0-9]#i', '', $city);
$city = mysql_real_escape_string($city);
$queryString .= "city='$city'";
}
} else {
$queryString .= '';
}
if ($_POST['state'] != '') {
if (($_POST['fname']) || ($_POST['city']) || ($_POST['country']) || ($_POST['email'])){
$state = $_POST['state'];
$state = stripslashes($state);
$state = strip_tags($state);
$state = preg_replace('#[^A-Za-z 0-9]#i', '', $state);
$state = mysql_real_escape_string($state);
$queryString .= " AND state='$state'";
} else {
$state = $_POST['state'];
$state = stripslashes($state);
$state = strip_tags($state);
$state = preg_replace('#[^A-Za-z 0-9]#i', '', $state);
$state = mysql_real_escape_string($state);
$queryString .= "state='$state'";
}
} else {
$queryString .= '';
}
if ($_POST['country'] != '') {
if (($_POST['fname']) || ($_POST['city']) || ($_POST['state']) || ($_POST['email'])) {
$country = $_POST['country'];
$queryString .= " AND country='$country'";
}
else {
$country = $_POST['country'];
$queryString .= "country='$country'";
}
} else {
$queryString .= '';
}
if ($_POST['email'] != '') {
if (($_POST['fname']) || ($_POST['city']) || ($_POST['state']) || ($_POST['country'])){
$email = $_POST['email'];
$email = stripslashes($email);
$email = strip_tags($email);
$email = preg_replace('#[^A-Za-z 0-9,[email protected]]#i', '', $email);
$email = mysql_real_escape_string($email);
$queryString .= " AND email='$email'";
} else {
$email = $_POST['email'];
$queryString .= "email='$email'";
}
} else {
$queryString .= '';
}
////////////// QUERY THE MEMBER DATA USING THE $queryString variable's value
$sql = mysql_query("SELECT id, username, firstname, city, state, country FROM members WHERE $queryString AND emailactivated='1' ORDER BY id ASC");
這個伎倆!僅供參考:我必須將「和」更改爲「AND」,並將連接格式設置爲「。 ='爲'。='讓它起作用。 – Veloj