显示用户输入值

时间:2012-09-08 18:03:02

标签: php

+-----+-------+--------+
  ID  | Owner | Number |
+-----+-------+--------+
 com1 | peter | 103045 |
+-----+-------+--------+
 com2 | deter | 103864 |
+-----+-------+--------+
嘿伙计们。我正在研究用户想要输入特定ID的项目的这一部分。当他输入这个特定的ID时,假设打印出ID中的所有行。在这种情况下,如果他输入com1,它将显示:

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

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

form.php的

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

query.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。有没有办法取消这条线,它仍然有效。谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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>

?>