php - 提交GET方法表单后,如何修复“on”和“search=+”字符未出现在Url上?
问题描述
为什么在提交 GET 方法表单后,我的 Url 上出现字符“On”和“search=+”?
这是我的表格:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div align='center'>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>?col=<?php echo "$col&search=$search&limit=$limit&page=1";?>">
<label for="search">Search</label>
<input type="text" name="search" id="search" required>
<br>
<label for="col">Search Type</label>
<input type="radio" name="col" id="keywords" required>Keywords
<input type="radio" name="col" id="keyphrase" required>Keyphrase
<br>
<label for="limit">Limit</label>
<select name="limit" id="limit">
<option value=""></option>
<option value="1">1</option>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="1000">1000</option>
</select>
<br>
<button type="submit" name="search_links" id="search_links" value=" ">Search Links</button>
<br>
<button type="reset">Reset</button><br>
<br>
<br>
</form>
提交后,我需要表单将用户发送到:http://localhost/test/pagination_2.php?search=$search&col=$col=limit=$limit&page=1
例如:http://localhost/test/pagination_2.php?search=heman&col=keywords&limit=2&page=1
search= keywords go here
col= the mysql tbl column to query
limit= search result per page
问题是,我被发送到:http://localhost/test/pagination_2.php?search=heman&col=on&limit=&search_links=+
注意“col=on”。没有名为“on”的 mysql tbl 列,因此出现错误:
致命错误:未捕获的 mysqli_sql_exception:您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在“on =?”附近使用的正确语法 在 C:\xampp\htdocs\test\pagination_2.php:90 的第 1 行堆栈跟踪:#0 C:\xampp\htdocs\test\pagination_2.php(90): mysqli_stmt_prepare(Object(mysqli_stmt), 'SELECT COUNT( su...') #1 {main} 在第 90 行的 C:\xampp\htdocs\test\pagination_2.php 中抛出
Q1。为什么浏览器显示“col=on”而不是“col=$col”(在本例中为“col=keywords”)?
Q2。另外,为什么在 url 中添加:“search=+”?如何摆脱它?我的代码:
<?php
if(ISSET($_SESSION['user_id']))
{
$user_id = $_SESSION['user_id'];
$user_type = 'member';
}
elseif(ISSET($_COOKIE['guest']))
{
$user_id = $_COOKIE['guest'];
$user_type = 'guest';
}
else
{
die("Error 1: Invalid User!");
}
if(ISSET($_GET['search']) && is_string($_GET['search']))
{
$search = $_GET['search'];
}
else
{
die("Type your single Keyword or single Keyphrase!");
}
if(ISSET($_GET['col']) && is_string($_GET['col']))//'col=search_type'. 'search_type' options: 1). Mysql Column:keywords; 2. Mysql Column:keyphrase.
{
$col = $_GET['col'];
}
else
{
echo "Select Checkbox!";
die("Select your search type to indicate whether you are searching for a single Keyword or a single Keyphrase!");
}
在表单上,我选择了“keywords”作为要查询的“col”:
<input type="radio" name="col" id="keywords" required>Keywords
<input type="radio" name="col" id="keyphrase" required>Keyphrase
Q3。如何修复以下行来解决问题?
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>?col=<?php echo "$col&search=$search&limit=$limit&page=1";?>">
Q4。需要在 url 中添加安全性,这样黑客就不能在 url 中注入。我需要在上述代码中添加 urlencode 吗?
解决方案
当以 GET REQUEST 形式提交表单时,某些特殊字符会导致问题,例如,如果您的文本中有 #(hash),那么在您的服务器脚本上读取它时,#(hash) 之后的文本将从 uri 中被忽略。
因此,为了避免这个问题,您应该将表单作为 POST 提交,或者使用 javascript 通过使用 encodeURIComponent() 对您的 url 查询参数值进行编码。
使用encodeURIComponent()的示例:
<script>
var uri = "https://example.com/test.php?name=ståle&car=saab";
var res = encodeURIComponent(uri);
</script>
在将其作为 GET REQUEST 提交到服务器端之前,您必须对所有表单值进行编码。
推荐阅读
- azure-pipelines - 在 Azure Pipeline 中查找项目构建服务的名称
- visual-studio-code - 在 vscode 上使用内置设置同步时,实际文件在哪里?
- distance - 在 GeoPandas 中获取两点之间的距离
- c# - 如何使用 LocalDB 和 SQL 连接为我的 Visual Basic WinForms 应用程序创建安装程序/设置项目?
- python - 设置 Django 日期字段时出现错误
- c# - 参考似乎不起作用,没有抛出明显的错误
- visual-studio-extensions - Visual Studio Marketplace 上私有扩展的可见性
- c++ - .so 文件的动态加载和对符号的引用
- mysql - 如果列的值为0,我如何选择表的行,否则如果sql中的值不是0,则左连接?
- javascript - 为本地测试设置 GitHub Actions 输入值