Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将结果发布回搜索表单所在的页面_Php_Search_Post_Pdo_Postback - Fatal编程技术网

Php 将结果发布回搜索表单所在的页面

Php 将结果发布回搜索表单所在的页面,php,search,post,pdo,postback,Php,Search,Post,Pdo,Postback,我正在做一个项目,需要一些帮助,请:)(完整的代码在底部) 项目需要通过PDO访问。 我需要搜索结果显示在搜索输入的同一页上 在我看来,用GET代替POST似乎不太合适。。这是正确的吗? 这是可行的,但我需要删除/隐藏第一次加载页面(index.php)时出现的这段代码 if(!isset($_GET['search'])) { echo "Error, Please go back."; exit;} 我该怎么做 另外,我的第二个问题是,我无法让搜索表单在表中搜索多个字段。它只是不让我

我正在做一个项目,需要一些帮助,请:)(完整的代码在底部)

项目需要通过PDO访问。
我需要搜索结果显示在搜索输入的同一页上

在我看来,用GET代替POST似乎不太合适。。这是正确的吗? 这是可行的,但我需要删除/隐藏第一次加载页面(index.php)时出现的这段代码

if(!isset($_GET['search']))
{   echo "Error, Please go back.";  exit;}
我该怎么做

另外,我的第二个问题是,我无法让搜索表单在表中搜索多个字段。它只是不让我。我也不能使用这段代码

%'.$searchterm.'%
因为它不会给我任何搜索反馈。所以我用的是

:searchterm

这是我的全部代码:

<?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;
?>

输入一个权重类。需要一个以上的搜索,这是行不通的

在良好实践中,当用户向服务器发送将更改服务器上数据的内容时,使用POST发送参数(例如存储在数据库中或发送电子邮件)。当用户从服务器检索数据时,使用GET来读取数据(查询数据库)。所以我宁愿到这里

为了解决您的问题,只需将处理研究的整个代码放在“if(isset($\u GET['search']){}”部分,如下所示:

<?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;
 }
?>

输入一个权重类。需要一个以上的搜索,这是行不通的

我觉得用GET代替POST似乎不合适。。这是正确的吗?这是可行的,但我需要删除/隐藏第一次加载页面(index.php)时出现的这段代码。

这取决于您希望何时使用
GET
POST
。POST更安全,所以对于提交表单,我总是使用
POST
。在这种情况下,您可以保留以下代码:

if(isset($_POST['search'])){
    echo 'Search';
}
您确实需要将表单的
操作
类型更改为
POST

<form action="index.php" method="post">
....

所以我想出来了

<!-- 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>"; }

输入体重等级或国籍:
如果(isset($_POST['search'])){
$enteredterm=$\u POST['enteredterm'];
如果($enteredterm==“”){
echo“错误,请输入内容。”;
}否则{
$stmt=$conn->prepare(“从boxer中选择*,其中weightclass如:enteredterm或national如:enteredterm或lastname如:enteredterm ORDER BY year”);
$stmt->bindValue(':enteredterm','%.$enteredterm'%');
$stmt->execute();
$count=$stmt->rowCount();
echo“您输入了”.$enteredterm.“并返回”;
if($count fetch(PDO::fetch_OBJ))
{echo“
    ”; 回声“; 回声“
”;}
LIKE%:searchterm%
并从BindValueDrop%这将永远不会发生
“”;
不完整。为什么不使用ajax将表单数据处理到服务器,并获取响应以在同一页面上再次显示它?如果将表单设置为post,则If应该可以正常工作。另外,与搜索相关的其余代码也应该封装在If中。
$searchterm = $_POST['search'];
<!-- 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>"; }