显示用户输入值


display user input value

+-----+-------+--------+
  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); 

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

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

尝试删除

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