如何使用 html 表单中的一个文本字段来搜索数据库表中的任何列
How to use one text field in html form to search any column in database table
我正在尝试了解如何使用表单中的一个文本字段来搜索数据库表中的任何列。
现在它只是在我的数据库表名为 stock 的 sn 列中搜索。我需要它来搜索列名 sn 或用户或状态。
因此,如果您输入序列号和发布表单,它将搜索我的数据库并毫无问题地返回结果。但是我希望能够键入用户名或状态并以这种方式搜索我的数据库。所以我在想一些如何动态改变输入名称=",也许带有单选按钮将值更改为输入名称="sn"或输入名称="用户"或输入名称="状态" 但我不确定该怎么做。 我一直在读其他人用Javascript或JQuery说。我真的很感激任何帮助。谢谢!
我的记录集只能查询 sn 列。 我确定我也需要改变这一点,但我不确定如何改变。
表格
<form id="form1" name="form1" method="get" action="search.php">
<label>
<input type="radio" name="RadioGroup1" value="sn" id="RadioGroup1_0" />
sn</label><br />
<label>
<input type="radio" name="RadioGroup1" value="user" id="RadioGroup1_1" />
User</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="status" id="RadioGroup1_2" />
Staus</label>
<br />
</p>
<label for="sn"> Search :</label>
<input name="sn" type="text" id="sn" size="25" />
<input name="submit" type="submit" value="submit" />
</form>
该 SQL
SELECT *
FROM stock
WHERE sn = colname
ORDER BY `datetime` DES
菲律宾语
<?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;
}
}
$maxRows_Recordset567 = 10;
$pageNum_Recordset567 = 0;
if (isset($_GET['pageNum_Recordset567'])) {
$pageNum_Recordset567 = $_GET['pageNum_Recordset567'];
}
$startRow_Recordset567 = $pageNum_Recordset567 * $maxRows_Recordset567;
$colname_Recordset567 = "-1";
if (isset($_GET['sn'])) {
$colname_Recordset567 = $_GET['sn'];
}
mysql_select_db($database_stock, $stock);
$query_Recordset567 = sprintf("SELECT * FROM stock WHERE sn = %s ORDER BY `datetime` DESC", GetSQLValueString($colname_Recordset567, "text"));
$query_limit_Recordset567 = sprintf("%s LIMIT %d, %d", $query_Recordset567, $startRow_Recordset567, $maxRows_Recordset567);
$Recordset567 = mysql_query($query_limit_Recordset567, $stock) or die(mysql_error());
$row_Recordset567 = mysql_fetch_assoc($Recordset567);
if (isset($_GET['totalRows_Recordset567'])) {
$totalRows_Recordset567 = $_GET['totalRows_Recordset567'];
} else {
$all_Recordset567 = mysql_query($query_Recordset567);
$totalRows_Recordset567 = mysql_num_rows($all_Recordset567);
}
$totalPages_Recordset567 = ceil($totalRows_Recordset567/$maxRows_Recordset567)-1;
?>
单选按钮是个好主意...确保它们都具有相同的名称。然后使用 php 来识别哪个值属于哪个操作。确保保护输入中的所有数据。您也可以使用 mysql 术语 OR 一次在不同的列中进行搜索。这是你的 php:
$radioVal = $_POST['radiobtnVal'];
switch $radioVal{
case 1 :
$col = "sn"; //don't directly write to the variable here, since that would make a security risk
break;
case 2 :
$col = "user";
break;
case 3 :
$col = "status";
break;
}
//now make connection to your database
$search = mysqli_real_escape_string($db,$_POST['searchString']); //This will secure your input from mysql injections
//$db is the variable you made during connection with your database
$result = mysqli_query($db,"SELECT * FROM `Stock` WHERE `" . $col . "`='" . $search . "'");
//or use the mentioned OR term
$result = mysqli_query($db,"SELECT * FROM `Stock` WHERE `sn`='" . $search . "' OR `user`='" . $search . "' OR `status`='" . $search . "'");
//Note that if some search strings are in multiple columns all these rows will be selected
我希望这有帮助
这是一种基本方法,它是一个很好的用户体验。试试这个解决方案。
形式
<Form action="search.php" method="post">
<label>Serial</label>
<input name='search' type='text' size='25'/>
<button type='submit'>Search</button>
</form>
.PHP
<?php
$search = $_POST['search'];
$searchColumns = array();
//Will add Serial Number if # was found on search
if(!(strpos("#",$search)===FALSE)){
$searchColumns[] = "sn = '{$search}'";
}
//Will add Status to select if there is no spaces in search.
if(count(explode(" ",$search))==1){
$searchColumns[] = "status = '{$search}'";
}
//Will add user to select if its not a Serial search
//And if its has more than 5 chars (change it to your min char username)
if(strpos("#",$search)===FALSE && strlen($search) > 5){
$searchColumns[] = "user = '{$search}'";
}
$sql = "
SELECT *
FROM stock
WHERE ".(implode(" AND ",$searchColumns))."
ORDER BY datetime DES";
?>
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 使用jquery将输入字段转换为文本
- 如何从查询字符串中的输入字段发回文本
- 表单输入字段随着溢出的文本而增长
- 使用单个文本框向多个字段添加严格搜索
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 使用jQuery的输入字段文本换行
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 当输入字段(文本框)有值时,选中/取消选中复选框
- 重置时输入字段文本css
- 将输入字段文本放入变量中并将其用于 jquery JSON get 请求
- 在 html 输入字段 (文本框) 中插入 javascript (jquery) 变量
- 搜索字段文本没有't在FF 3.6中显示
- 主干集合url操作与输入字段文本
- 如何大写输入字段文本的第一个字母,而键入javascript
- 如何使用angularjs在同一字段中键入时动态格式化输入字段文本
- 如何将输入字段(文本框)添加到使用jQuery动态创建的表中