0
+-----+-------+--------+
  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!

6
  • use $input = $_POST['id'] as Rohan Sood suggests, when casting to an integer and there are characters in the string, the characters will be left out, therefor no ID will match com1 because it will result in just 0 when casting to an integer. Commented Sep 8, 2012 at 18:09
  • 1
    Put $input in single quotations like: "SELECT * FROM system_audit WHERE ID = '$input' " Commented Sep 8, 2012 at 18:11
  • hi siamak and dbf. thank you but i have tried both before. it returns the following message and shows no result Warning:mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\query.php Commented Sep 8, 2012 at 18:12
  • @user1629618 this error happens when it's something wrong with the query string or mysql connection or database selection. make sure that you connected to mysql and select table before the query string and then check the table name "system_audi" and the col name "ID". Commented Sep 8, 2012 at 18:19
  • @user1629618 - try echoing out your generated query and entering it directly in the database. Commented Sep 8, 2012 at 18:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Please tell us the result for a print_r($row);
echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Type'] . "</td>"; echo "<td>" . $row['Setting'] . "</td>"; echo "<td>" . $row['Value'] . "</td>"; echo "<td>" . $row['Actual'] . "</td>"; echo "</tr>"; is just to put them in table form
I got that bro. Just in your document, query.php, put in a "print_r($row)" right after the opening braces for the while statement. And tell us the output.
OH! pardon me. i havent slept properly for a few days. and nah. it didnt work. says there's a systax error
What is your result for <?php echo "SELECT * FROM system_audit WHERE ID = $input";?>
|
0

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>

?>

2 Comments

mysql_fetch_row is generally considered as a relatively better practice due to the increased readability of the code. So I wouldn't really recommend your approach, even though it might just get the work done.
hey again rohan. mm. you said to replace $query to $result. mm, the thing is i did not declare any $result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.