如何在搜索查询中访问php文件?

时间:2013-12-17 17:36:24

标签: php mysql wordpress

好的,所以我做了一个简单的搜索功能,连接到MySQL服务器。 根据我的理解,这样做的方法是:

  • 在网站上创建搜索页面。
  • 然后,搜索页面将接受查询并在您提供的.php中运行
  • 结果页面将会出来。

这是我将使用的基本股票代码:

<html>
 <body>
 <form action="searchtemplate.php" method="post">
 Part ID: <input type="text" name="partID" />
 <input type="submit" /></form>&nbsp;

</body>
</html>

我从w3学校网站得到了这个...它很简单而且很有效。 我正在使用的php文件是:

<?php
/**
 Template Name: My Template
 */

get_header(); ?>

<div class="clearfix left-sidebar">

<!--BEGIN #primary-->
<div id="primary">

<?php the_post(); ?>

<?php $con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno())
  {
  throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
  }

$part_query = $_POST['partID'];
$result = mysqli_query($con,"SELECT * FROM Parts WHERE part_id = $part_query"); 

echo "<table border='1'>
<tr>
<th>part_id</th>
then rest of columns 

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['part_id'] . "</td>";
  then echo rest of columns

echo "</tr>";
  }
echo "</table>"; 


mysqli_close($con); ?>


        <!--END #primary-->
        </div>

        <?php get_sidebar(); ?>

    </div>

<?php get_footer();
?> 

好的,我正在使用wordpress,我在主题目录中设置了这个php,它可以作为模板访问。我的主要问题是搜索页面显示正常,但是当我点击提交时,它会将我带到一个空白页面而不是结果页面。我想知道这是否是我正在努力完成的事情的正确方法,或者是否有更好的方法。

1 个答案:

答案 0 :(得分:2)

您的表单可能会发布到模板searchtemplate.php,而不是使用模板执行代码的Wordpress页面。

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

表单标记中的“action”参数应指向您网站上使用自定义模板的实际网页。