使用POST提交表单后未显示数据

时间:2014-11-07 22:45:59

标签: php mysql forms

我有一个使用POST方法的表单,在输入名称并单击搜索后,它应该显示数据库中的信息,以及您使用的术语。

的index.php

<form action="search.php" method="POST">
    <input type="text" name="search" /><br/><br/>
    <input type="Submit" value="Search" />
</form>

的search.php

    <?php
 if (!$_POST) {
    include('index.php');
    } else {
    ?>
<h1>Server name<br />Official ItemDB!</h1>
<br />
<?php

    $search = $_POST["search"];
        MySQL_connect("localhost", "pernix_items", "#");
        MySQL_select_db("pernix_items");
        $result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'");

while($row = mysql_fetch_array($result));
  {
  ?>
  <table border="1">
<?
    echo '<tr>';
    echo '<td>'.$row['id'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['desc'] . '</td>';
    echo '</tr>';
        }
    }
?>  
</tr>
</table>

我添加了mysql_error();在我出错的地方开展工作,到这个

$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'" or die(mysql_error()));

并给我这个

  

警告:mysql_fetch_array()要求参数1为资源,布尔值在第21行的/home1/pernix/public_html/tools/item-list/search.php中给出

我在search.php的第21行

while($row = mysql_fetch_array($result));

所以我添加了另一个或模具打印错误。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

虽然在评论中已经解决,但在此处将其作为一个整体回答。

首先or die()行是错误的,因为die语句在<{>} mysql_query内执行,而不是在查询失败的时候,因为它应该是:

$result = mysql_query("SELECT * FROM items WHERE name LIKE '%" . mysql_real_escape_string($search) . "%'") or die(mysql_error());

其次,在while()线制作之后你有一个额外的分号,应该删除它们看起来像:

while($row = mysql_fetch_array($result))
{}

最后也是最重要的是,您应该将此代码真正转换为mysqlipdo,因为从PHP 5.5.0开始不推荐mysql扩展名,并且将来会被删除。