Web Design

(Nancy Kaufman) #1

309


CHAPTER

Adding Information from a Database 13


Why is the search variable in single quotation
marks in the query?
SQL requires that references to data in nonnumeric
fields be placed in single quotation marks in
queries. If the field on which you are searching
contains numeric data, you would omit the quotes.

Can I allow my user to search on more than one
field?
Yes, although doing so significantly complicates
your query. If you have two fields, you must account
for four possibilities: that your user enters
information into both fields, neither field, only the
first, or only the second. Although doing so is
possible, it can be difficult.

! Type $search =
$_POST[‘search’];.


@ Type $query = “SELECT
field1, field2 FROM
table WHERE field1 =
‘$search’”;, replacing
field1 and field2 with the
database fields you want to
return and table with the
table name.


The query is created.


8 In your editor, open a new
PHP document.


9 Within the body of the
document, type <?php.


0 Type include(“?”);,
replacing? with the path to
the database connection
page.


8


9


10


11


12


TIPS

Free download pdf