我正在做一个项目并需要一些帮助请:)(底部的完整代码)
需要使用PDO访问项目 我需要将搜索结果显示在输入搜索的同一页面上。
以下对我来说似乎没有使用GET而不是POST ..这是正确的吗? 这有效但我需要删除/隐藏我的页面(index.php)首次加载时出现的这段代码。
if(!isset($_GET['search']))
{ echo "Error, Please go back."; exit;}
我该怎么做?
另外我的第二个问题是我无法通过搜索表单搜索表格中的多个字段。它只是不会让我。我不能使用这段代码
%'.$searchterm.'%
因为它不会从搜索中给我任何反馈。所以我正在使用
:searchterm
在
$searchterm = $_GET['search'];
$stmt = $conn->prepare("SELECT * FROM boxer WHERE weightclass LIKE :searchterm OR nationality ");
$stmt->bindValue(':searchterm','%'.$searchterm.'%');
$stmt->execute();
这是我的完整代码:
<?php
$servername = 'localhost';
$username = "root";
$password = "";
$dbname = "u1360138";
<?php
if(isset($_POST['search'])){
echo 'Search';
}
?>
<!-- Search facility 1 -->
<form action="index.php" method="get">
<label for="search">Enter a weight class. Need to be more than one searchs which wont work</label>
<input type="text" name="search" id="search">
<input type="submit" value="Search">
</form>
<?php
// DB Connection
try {$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);}
catch(PDOException $e)
{echo "Error conntecting to the DB: " . $e->getMessage();}
if(!isset($_GET['search']))
{ echo "Error, Please go back."; exit;}
// DB Connection
$searchterm = $_GET['search'];
$stmt = $conn->prepare("SELECT * FROM boxer WHERE weightclass LIKE :searchterm");
$stmt->bindValue(':searchterm','%'.$searchterm.'%');
$stmt->execute();
// loop displays loop
while ($boxer = $stmt->fetch(PDO::FETCH_OBJ))
{ echo "<ul>";
echo "<a href='details.php?idboxer=".$boxer->idboxer."'>";
echo "<li>".$boxer->firstname." ".$boxer->lastname."</li>";
echo "</a>";
echo "</ul>"; }
$conn=NULL;
?>
答案 0 :(得分:0)
在良好实践中,当用户向服务器发送内容以更改服务器上的数据时,使用POST发送参数(以db为例进行存储或发送电子邮件)。当用户从服务器中检索某些内容时,使用GET来读取数据(查询数据库)。所以更喜欢GET。
要解决您的问题,只需将处理研究的整个代码放在&#34; if(isset($ _ GET [&#39; search&#39;])){}&#34;部分如下:
<?php
$servername = 'localhost';
$username = "root";
$password = "";
$dbname = "u1360138";
<?php
if(isset($_GET['search'])){
echo 'Search';
}
?>
<!-- Search facility 1 -->
<form action="index.php" method="get">
<label for="search">Enter a weight class. Need to be more than one searchs which wont work</label>
<input type="text" name="search" id="search">
<input type="submit" value="Search">
</form>
<?php
if(isset($_GET['search'])){
// DB Connection
try {$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);}
catch(PDOException $e)
{echo "Error conntecting to the DB: " . $e->getMessage();}
// DB Connection
$searchterm = $_GET['search'];
$stmt = $conn->prepare("SELECT * FROM boxer WHERE weightclass LIKE :searchterm");
$stmt->bindValue(':searchterm','%'.$searchterm.'%');
$stmt->execute();
// loop displays loop
while ($boxer = $stmt->fetch(PDO::FETCH_OBJ))
{
echo "<ul>";
echo "<a href='details.php?idboxer=".$boxer->idboxer."'>";
echo "<li>".$boxer->firstname." ".$boxer->lastname."</li>";
echo "</a>";
echo "</ul>";
}
$conn=NULL;
}
?>
答案 1 :(得分:0)
以下对我来说似乎不适合使用GET而不是POST ..这是正确的吗?这有效但我需要删除/隐藏我的页面(index.php)首次加载时出现的这段代码。
这取决于您何时使用GET
或POST
。 POST更安全,因此提交表单时我总是使用POST
。在这种情况下,您可以保留此代码:
if(isset($_POST['search'])){
echo 'Search';
}
您需要将表单的action
类型更改为POST
:
<form action="index.php" method="post">
....
然后添加您需要从POST
而不是GET
获取搜索值所需的结尾,因为我们更改了action
类型。
$searchterm = $_POST['search'];
答案 2 :(得分:0)
所以我想出了这个和
<!-- HTML FORM SEARCH BAR -->
<form action="index.php" method="post">
<label for="enteredterm">Enter a Weight-class or a Nationality:</label>
<input type="text" name="enteredterm">
<input type="submit" name="search">
</form>
<!-- HTML FORM SEARCH BAR -->
if(isset($_POST['search'])){
$enteredterm = $_POST['enteredterm'];
if ($enteredterm ===""){
echo "error, enter something.";
} else {
$stmt = $conn->prepare("SELECT * FROM boxer WHERE weightclass LIKE :enteredterm OR nationality LIKE :enteredterm or lastname LIKE :enteredterm ORDER BY year");
$stmt->bindValue(':enteredterm','%'.$enteredterm.'%');
$stmt->execute();
$count= $stmt->rowCount();
echo "You entered ".$enteredterm." and returned ";
if($count <= 1){
echo $count." result.";
}else{
echo $count." results.";
}
// loop displays loop
while ($boxer = $stmt->fetch(PDO::FETCH_OBJ))
{ echo "<ul>";
echo "<a href='details.php?idboxer=".$boxer->idboxer."'>";
echo "<li>".$boxer->firstname." ".$boxer->lastname."</li>";
echo "</a>";
echo "</ul>"; }