0
+-----+-------+--------+
  ID  | Owner | Number |
+-----+-------+--------+
 com1 | peter | 103045 |
+-----+-------+--------+
 com2 | deter | 103864 |
+-----+-------+--------+

大家好。我正在处理项目的这一部分,假设用户输入某个 ID。当他输入这个特定的 ID 时,假设打印出该 ID 行中的所有内容。在这种情况下,如果他输入 com1,它将显示:

+-----+-------+--------+
  ID  | Owner | Number |
+-----+-------+--------+
 com1 | peter | 103045 |
+-----+-------+--------+

我使用了这个表格和代码:

表单.php

    <form action="query.php" method="POST">
    Enter your ID: <input type="varchar" name="id" /><br />
    <input type="submit" value="Audit" />
    </form> 

查询.php

   $con = mysql_connect("localhost", "root", "");
    if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

    mysql_select_db("livemigrationauditingdb", $con);


    $input = (int) $_POST['id'];
    $query = mysql_query("SELECT * FROM system_audit WHERE ID = $input");

    echo "<table border='1'>
    <tr>
    <th>ID</th>
    <th>Owner</th>
    <th>Number</th>
    </tr>";


    while($row = mysql_fetch_array($query))
    {
      echo "<tr>";
      echo "<td>" . $row['ID'] . "</td>";
      echo "<td>" . $row['Owner'] . "</td>";
      echo "<td>" . $row['Number'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    mysql_close($con); 

认为你们知道错误吗?哦,我是否可以不将其链接到其他页面?我需要将表格和代码放在一页中。目前我的表单链接到 query.php。有没有办法取消这条线,它仍然有效。谢谢!

4

2 回答 2

1

尝试从 $input = (int) $_POST['id']; 中删除 (int)

于 2012-09-08T18:07:02.670 回答
0

尝试删除

while($row = mysql_fetch_array($query)) 

并将其替换为

while($row = mysql_fetch_row($result))

还要删除 (int) 并查看它是否有帮助。

  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['Owner'] . "</td>";
  echo "<td>" . $row['Number'] . "</td>";

可以换成

  echo "<td>" . $row[0] . "</td>";
  echo "<td>" . $row[1] . "</td>";
  echo "<td>" . $row[2] . "</td>";

您也可以将它们都放在同一页面上,如图所示

<?php

    $con = mysql_connect("localhost", "root", "");
    if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

    mysql_select_db("livemigrationauditingdb", $con);


    $input = (int) $_POST['id'];

    if($input)
    {
      $query = mysql_query("SELECT * FROM system_audit WHERE ID = $input");

      echo "<table border='1'>
      <tr>
      <th>ID</th>
      <th>Owner</th>
      <th>Number</th>
      </tr>";


      while($row = mysql_fetch_row($query))
      {
        echo "<tr>";
        echo "<td>" . $row[0] . "</td>";
        echo "<td>" . $row[1] . "</td>";
        echo "<td>" . $row[2] . "</td>";
        echo "</tr>";
      }
      echo "</table>";
    }

    mysql_close($con); 
?>   

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    Enter your ID: <input type="varchar" name="id" /><br />
    <input type="submit" value="Audit" />
    </form>

?>
于 2012-09-08T18:12:31.003 回答