我有一个包含8列的表的数据库。 我的网站上还有一个搜索页面,其中有8个文本框(对应于数据库中的8列),用户可以在其中输入一些值来检索具有指定条件的数据。
如果我在SQL查询中使用AND运算符,则用户必须为所有8个框指定约束。
我想要做的是将AND运算符应用于用户输入某些值的框并忽略其他空框..任何想法我该怎么做?
我正在使用Dreamweaver的GUI,并附上了代码。 谢谢!
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$varID_Recordset1 = "0";
if (isset($_GET['ID'])) {
$varID_Recordset1 = $_GET['ID'];
}
$varFirstName_Recordset1 = "-1";
if (isset($_GET['First_Name'])) {
$varFirstName_Recordset1 = $_GET['First_Name'];
}
$varLastName_Recordset1 = "-1";
if (isset($_GET[Last_Name'])) {
$varLastName_Recordset1 = $_GET[Last_Name'];
}
$varCGPA_Recordset1 = "0.00";
if (isset($_GET['GPA'])) {
$varCGPA_Recordset1 = $_GET['GPA'];
}
$varGender_Recordset1 = "-1";
if (isset($_GET['Gender'])) {
$varGender_Recordset1 = $_GET['Gender'];
}
$varNationality_Recordset1 = "-1";
if (isset($_GET['Nationality'])) {
$varNationality_Recordset1 = $_GET['Nationality'];
}
$varAdvisor_Recordset1 = "-1";
if (isset($_GET['Advisor'])) {
$varAdvisor_Recordset1 = $_GET['Advisor'];
}
$varEnroll_Recordset1 = "-1";
if (isset($_GET['Enrollment_Year])) {
$varEnroll_Recordset1 = $_GET['Enrollment_Year];
}
mysql_select_db($database_databasestudents, $databasestudents);
$query_Recordset1 = sprintf("SELECT students.ID, students.FirstName, students.LastName FROM students WHERE ID= %s AND FirstName= %s AND LastName=%s AND GPA=%s AND Gender = %s AND Nationality= %s AND Advisor= %s AND EnrollmentYear= %s", GetSQLValueString($varID_Recordset1, "int"),GetSQLValueString($varFirstName_Recordset1, "text"),GetSQLValueString($varLastName_Recordset1, "text"),GetSQLValueString($varCGPA_Recordset1, "double"),GetSQLValueString($varGender_Recordset1, "text"),GetSQLValueString($varNationality_Recordset1, "text"),GetSQLValueString($varAdvisor_Recordset1, "text"),GetSQLValueString($varEnroll_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $databasestudents) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>