Re: PHP Date Search Question



Greg (greg@xxxxxxxxxxx) decided we needed to hear...
> I have a page that searches a database by a repairman's name and by a
> date range. It pulls info by the repairman's name but pulls all info in
> the database regardless of the date. Below is the code of the two
> pages. What am I missing?
<snip>

I've noted a few problems below - there may be more but I only gave it
a fairly quick look.

Lack of indentation makes your code hard to read, and all those
multi-line echo with all the HTML make matters worse IMO.

Your queries look like they are valid, but without seeing the table
definition, and samples of your input, its tough to guess why all dates
and not those you expect are returned.

> <?php
> $search = $_REQUEST['name'];
> $from = $_REQUEST['from'];
> $to = $_REQUEST['to];

You don't validate any of the above vars. What happens if someone
enters invalid dates or other nonesense instead of dates? What
happens if from date comes after to date?

> if($search) // perform search only if a string was entered.

It is best to use isset on your $_REQUEST variables, *then* validate
and use their contents.
You use $_REQUEST which gives both GET and POST variables, but your
form is set to POST. You should really be using $_POST.

> {
> mysql_connect("192.168.1.8","root","passwordhere");
> mysql_select_db("repair"); //database name
> $srch = "%".$search."%";
> $query = "SELECT * FROM repair WHERE repairman LIKE '$srch' AND
> daterepaired BETWEEN '$from' AND '$to'";

from and to are not validated to it would be easy for someone to
delete your data via SQL-injection.

> $total = "SELECT SUM(price) as price FROM repair WHERE repairman LIKE
> '$srch' AND daterepaired BETWEEN '$from' AND '$to'";
> $result = mysql_db_query("repair", $query) or die("MySQL error
> #".mysql_errno().":".mysql_error());

You've already selected the repair database above, so you might as
well use mysql_query() instead.

> $addtotal = mysql_db_query("repair", $total) or die("MySQL error
> #".mysql_errno().":".mysql_error());
> if ($result)

Consider testing mysql_num_rows(). You've already determined that the
query worked (it dies on error), so you may as well proceed based on
number of rows returned.

> {
> echo "<font face='Tahoma' size=2><P><CENTER><IMG SRC='images/MCLogo.jpg
> WIDTH='576' HEIGHT='87' NATURALSIZEFLAG='0'
> ALIGN='BOTTOM'></CENTER></P></font>
> <br>
> <br>
> echo "<table border=0 cellpadding=3 cellspacing=5>
<snip some code>
> <td width=10><font size=2 color=#FFFF00
> face=tahomo><b>M&R</b></font></td>
> </tr>;

There is a " missing in the line above.

>
> while ($r = mysql_fetch_array($result)) {//Begin while
<snip some code>
> <?
> include("footer.php";

There is a ) missing above. Did you copy/paste or retype this code? It
won't run at all in its present form.

> ?>
<snip>

--
Dave <dave@xxxxxxxxxxxxxxxxx>
(Remove REMOVE for email address)
.



Relevant Pages

  • Re: Size matters to some
    ... The underlying remote database connection ... physical layer is actually managing it all, the logical layer is forced to ... constraints as specified by developers. ... want to validate the information getting written. ...
    (comp.databases.pick)
  • Re: ODB (Cache?) vs ORM
    ... This is common and accepted database knowledge. ... > other way is error-prone, thus virtually guaranteed to break integrity, producing ... > is multiple orders of magnitude more powerful than procedural optimization. ...
    (comp.lang.java.databases)
  • Re: Evaluating D2005, help needed.
    ... > quality of Delphi still hasn't crossed below the productivity of Visual ... and had built several database test cases to backup your claims but now it seems ... Delphi for Win32 and VS.NET for .NET and while I'm quite confident that my ...
    (borland.public.delphi.non-technical)
  • Re: Please help: Numeric class properties
    ... > that reflects a table in my database. ... I wish to validate the property values before writing them ... TextBox entry BEFORE you attempt to assign it anywhere? ... Function IsDigitsOnly(Value As String) As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Re: OOP database tables <-> php interface (semi LONG)
    ... The Access database has over 30 ... database and business object layers. ... Good point about the flexibility too! ...
    (comp.lang.php)