Computing.Net > Forums > Web Development > Return Message on PHP mysql query

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Return Message on PHP mysql query

Reply to Message Icon

Name: RTAdams89
Date: May 21, 2008 at 18:12:13 Pacific
OS: na
CPU/Ram: na
Product: na
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: RTAdams89
Date: May 21, 2008 at 18:13:49 Pacific
Reply:

PS: you can download the whole PHP script here:

http://invoices.ryantadams.com/inde...

-Ryan Adams
http://RyanTAdams.com


0

Response Number 2
Name: Michael J (by mjdamato)
Date: May 22, 2008 at 10:06:21 Pacific
Reply:

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


0

Response Number 3
Name: RTAdams89
Date: May 22, 2008 at 12:24:25 Pacific
Reply:

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


0

Response Number 4
Name: Michael J (by mjdamato)
Date: May 22, 2008 at 19:47:58 Pacific
Reply:

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


0

Response Number 5
Name: RTAdams89
Date: May 23, 2008 at 01:27:33 Pacific
Reply:

Ahh, you are right on. I was thinking of a different command. That should do it. Perfect, thanks.

-Ryan Adams
http://RyanTAdams.com


0

Related Posts

See More



Response Number 6
Name: Michael J (by mjdamato)
Date: May 23, 2008 at 09:14:38 Pacific
Reply:

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


0

Response Number 7
Name: RTAdams89
Date: May 23, 2008 at 14:21:43 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Return Message on PHP mysql query

Error Running MySQL Query With PHP www.computing.net/answers/webdevel/error-running-mysql-query-with-php/960.html

suppress PHP, mysql warnings www.computing.net/answers/webdevel/suppress-php-mysql-warnings/1221.html

How do I setup PHP & MySQL on XP www.computing.net/answers/webdevel/how-do-i-setup-php-amp-mysql-on-xp/122.html