user1629618
user1629618

Reputation: 11

display user input value

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

Hey guys. I am working on this part of the project where the user is suppose to input a certain ID. When he input this certain ID it is suppose to print out everything in the row w that ID. In this case if he inputs com1, it will display:

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

I used this form and code:

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

think you guys know the mistake? oh and is there away for me not to link it to other page? i need the form and codes to be in one page. currently my form links to query.php. is there a way to cancel this line and it still works. thanks!

Upvotes: 0

Views: 248

Answers (2)

Rahul Kadukar
Rahul Kadukar

Reputation: 958

Try removing

while($row = mysql_fetch_array($query)) 

and replace it by

while($row = mysql_fetch_row($result))

Also remove the (int) and see if it helps.

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

Can be replaced by

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

You can also have both of them on the same page as shown

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

?>

Upvotes: 0

Rohan Sood
Rohan Sood

Reputation: 437

Try removing the (int) from $input = (int) $_POST['id'];

Upvotes: 1

Related Questions