Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a simple form which allows a visitor to enter a invoice number. When they submit the form the invoice number is passed to a PHP script which looks it up in a mysql data base and returns other data from the same row as the entered invoice number.
If a visitor enters an invoice number that isn't in the table, I would like to display a message to that effect. I have tried al kinds of php "if" statements using functions ranging from "isset()" to "empty()" but none work. What is the simple way to accomplish my goal? Here is the PHP code I currently have:
<?php
include "config.php";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?><html>
<head>
<title>Customer Invoice</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<center>
View Invoice<form action="" method="post">
Please enter your invoice number and then press "View".<br />
<input type="text" name="invoice" size="5">
<input type="submit" name="submit" value="View">
</form>
<br />
<br />
<br />
Admin Login
</center>
<?php
} else {
$invoice = mysql_real_escape_string($_POST['invoice']);
$result = mysql_query("SELECT * FROM invoices WHERE invoice='" . $invoice . "'");while($row = mysql_fetch_array($result))
{
echo "<center>Invoice # " . $row['invoice'] . "</center>";
echo "<center>" . $row['name'] . "</center>";
echo "<center>" . $row['date'] . "</center><br /><br />";
echo "Hours: " . $row['hours'] . "<br />";
echo "Rate: $" . $row['rate'] . "/hr<br />";
echo "Labor Charge: $" . ($row['hours'] * $row['rate']) . "<br /><br />";
echo "Hardware: " . $row['h1'] . " -$" . $row['c1'] . "<br />";
echo "Hardware: " . $row['h2'] . " -$" . $row['c2'] . "<br />";
echo "Hardware: " . $row['h3'] . " -$" . $row['c3'] . "<br />";
echo "Parts Charge: $" . ($row['c1'] + $row['c2'] + $row['c3']) . "<br /><br />";
echo "Total Due: $" . $row['total'] . " Paid: $" . $row['paid'] . " Balance Due: $" . ($row['total'] - $row['paid']) . "<br /><br />";
echo "Notes: " . $row['notes'];
echo "<br /><br />END OF INVOICE<br />";
echo "Click here to search again.";}
}
?></body>
</html>-Ryan Adams
http://RyanTAdams.com

PS: you can download the whole PHP script here:
http://invoices.ryantadams.com/inde...
-Ryan Adams
http://RyanTAdams.com

Although I would do things a lot differently, the function you are looking for is mysql_num_rows(). Added code in blue
====================================$result = mysql_query("SELECT * FROM invoices WHERE invoice='" . $invoice . "'");if (!mysql_num_rows($result)) {
echo "No records found";
} else {
while($row = mysql_fetch_array($result))
{
....
=======================================
Need to add a closing bracket for the else.Michael J

I thought about doing that, but won't that put unnecessary strain on the database? Each time someone checks their invoice, it has to count every row in the table (which could be thousands).
I'm I over estimating the problem?
-Ryan Adams
http://RyanTAdams.com

I think you have a misinterpretation of what mysql_num_rows() does. It does NOT count the rows in the database. It tells you haw many records were returned in a query.
In your example above you run a query against the database for records matching a particular invoice number. i would assume that invoice numbers are not duplicated. So the results of that query would include 1 record or 0 records. So, mysql_num_rows() will simply return a 1 or 0 on that result.
That is the exact purpose that command is used for. I'm sure you could do a different workaround using only PHP, but I doubt it would be more efficient.
Michael J

Ahh, you are right on. I was thinking of a different command. That should do it. Perfect, thanks.
-Ryan Adams
http://RyanTAdams.com

If you were thinking of the COUNT() command that you would use directly in the query, then that is not an intensive process either. Remember the data in a database is indexed so much of the information can be accessed very quickly.
Michael J

I'm not sure what I was thinking of, obviously something incorrect :). Anyhow, I've got the page working as I would like now. Thanks for the help!
-Ryan Adams
http://RyanTAdams.com

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |