doumao6212 2014-11-07 22:45
浏览 42
已采纳

使用POST提交表单后未显示数据[关闭]

I have a form using a POST method, upon enter a name and clicking search, it is supposed to display the information from the database, with the term you used.

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>

I added the mysql_error(); function to dertime where I was going wrong, to this

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

and gives me this

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/pernix/public_html/tools/item-list/search.php on line 21

My line 21 of search.php

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

So I added another or die print error.

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

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongzhuang1923 2014-11-07 23:07
    关注

    Although resolved in the comments, reposting it here as a whole answer.

    First the or die() line is wrong, as the die statement is executed inside the mysql_query and not as supposed to when and if the query fails, as it should be:

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

    Secondly you have an extra semicolon right after the while() line making, which should be removed to look like:

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

    Last and most importantly you should really convert this code to mysqli or pdo as the mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法