Another (basic) question here. I need to know how to use html forms to filter the results of a mysql query using PHP. I have been looking at w3schools and I can see how using $_POST on one page can output to another page. But I cannot see exactly how to put the $_POST into my query. For example I have one page as below:
<form action="orderlist.php" method="post">
OrderNo: <input type="int" name="order_no" />
<input type="Submit" />
</form>
This is a page where the user can enter an order_no and click submit, which links to the 'orderslist.php' page. On that page I have the following code:
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "</tr>";
}
echo "</table>";
This outputs everything from 'orders' into a table, but I want it to only output the row with the order_no that the user entered on the previous page. How do I do this? Is it something like:
$result = mysql_query("SELECT * FROM orders WHERE order_no = $_POST[order_no]");
Thank a lot
Your form should be something like this:
To get any value that is typed by user in the form you should use
type="text"
. There is nothing liketype="int"
in standard HTML.Don't get confused, the Input TYPE in HTML is not the same as one you use in Programming languages to declare Data type. Here TYPE is just to let the browser know that its a text field / Radio Button etc. To understand Valid Input Types better read this w3.org recommendation on HTML forms.
On
orderlist.php
you can query to retrieve the required data:Note:
This type of code is Vulnerable to easy attacks, and it is never recommended to put user input directly into SQL query, it should always be filtered first.
Try this:
This takes the value of
$_POST['order_no']
and somewhat sanitizes it. You then apply the value of$orderNumber
to MySQL.However, you're much better off using PDO or MySQLi. Both protect you (if used correctly) from SQL injection. Currenly, your code is VERY prone to SQL injection.
Without injection vulnerability (require 'mysql_connect' before) :
For starters, you should switch to PDO or mysqli -- the
mysql_*
functions are in the process of deprecation.On to your question: yes, that's how you do it, with a
WHERE
clause. However, it is very (very very very) insecure to concatenate a variable directly out of$_POST
without sanitation.That said, at a minimum you should do this:
Another thing... don't use
SELECT *
. You should always list the columns you expect to get from the database -- that way, if there is a problem (like the columns have changed), the query can let you know. WithSELECT *
you get back a magical grab-bag of data -- you have no idea what it is, and more importantly, if it has the values your code relies on.So, putting it together:
Edit: One last note, the input you're using is not valid;
type="int"
is not a recognized input type. Here's a list of types for HTML 4, and a list for HTML 5:As you can see, HTML 5 does have a new input type "number". It is not fully adopted, but if you wanted to use it:
<input type="number" name="order_no" />
Be aware, however, that not all browsers will restrict the input to numeric: http://caniuse.com/#feat=input-number
Documentation
SELECT
- http://dev.mysql.com/doc/refman/5.0/en/select.htmlmysql_query
(DEPRECATED) - http://php.net/manual/en/function.mysql-query.phpPDO::prepare
- http://www.php.net/manual/en/pdo.prepare.phpPDOStatement::fetch
- http://www.php.net/manual/en/pdostatement.fetch.phpPDOStatement::execute
- http://www.php.net/manual/en/pdostatement.execute.php