comparing form to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adult.swim
Forum Newbie
Posts: 9
Joined: Sun Jul 09, 2006 8:00 pm

comparing form to database

Post by adult.swim »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Im trying to:

1. create a form page in HTML

2. create a .php page that queries an ODBC database that I have, and compares info submitted 

in the form to info in my database.

My question is how can i make the comparison between my form and my databse. 

form.html
[syntax="html"]<form action="process.php" method="get">
mlsnumber: 
  <input name="mlsnumber" type="text" />
<input name="" type="submit" />
</form>
process.php--- "this is what i have so far...[/syntax]

Code: Select all

# connect to a DSN "homep_props" with a user and password "" 
$connect = odbc_connect("homep_props", "", "");

# query the users table for *
$query = "SELECT * FROM props";

# perform the query
$result = odbc_exec($connect, $query);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The submitted data will be in $_GET['mlsnumber'] .. If you understand how to construct strings, then you may use that to build your query to the database.
adult.swim
Forum Newbie
Posts: 9
Joined: Sun Jul 09, 2006 8:00 pm

strings

Post by adult.swim »

I know how to create strings, but I don't know how to use that in my query.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

make your query something like this

Code: Select all

SELECT * FROM tablename WHERE fieldname = "fieldcontents"
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

then something like

Code: Select all

if($result['fieldname'] == $fieldcontents)
{
     //do wht you want to do
}
adult.swim
Forum Newbie
Posts: 9
Joined: Sun Jul 09, 2006 8:00 pm

more over

Post by adult.swim »

I previously did this using coldfusion w/ a sql query.

I just added a WHERE clause to the sql that compared the two.

----------------------
<cfquery datasource="homep_props" name="checkForMatching">
SELECT *
FROM props
WHERE props.MLSnumber='#FORM.MLSnumber#'
ORDER BY props.MLSnumber</cfquery>
--------------------------

props.mlsnumber being the database field for mlsnumber, and FORM.mlsnumber being the form input by user.

I was wondering, can PHP being used similarly?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, PHP can do what you are asking. dull1554's example seems like it is using register_globals, so I would stear you from that first of all. You want to grab the data from the form using the $_GET array (since you are using 'get' as your form method). Then I would say to read that $_GET var into the string var that dull1554 posted and you should be golden.

Code: Select all

<?php
$mlsnumber = $_GET['mlsnumber']; // mlsnumber in the $_GET array is the field name from the form

// Spend some time here checking and validating your form data

//Run the query
$sql = "SELECT *
        FROM props
        WHERE props.MLSnumber = '$mlsnumber'
        ORDER BY props.MLSnumber";

//Finish up with your appropriate db functions for processing the query and fetching results
?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

what i posted was ment not as real code but just an example "frame" for a query and compairison. register_globals == VERYBAD :)
adult.swim
Forum Newbie
Posts: 9
Joined: Sun Jul 09, 2006 8:00 pm

hmm

Post by adult.swim »

Would I be able to use:

$fmlsnumber = $_GET['mlsnumber']; (form input mlsnumber)

------and-------

# fetch the data from the database
while(odbc_fetch_row($result)){
$mlsnumber = odbc_result($result, 2); (database mlsnumber; the location of it in the database)

# print("$mlsnumber \n");
}

to somehow compare the two?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

adult.swim, please use the syntax highlighting controls our forum provides. If you don't know how, read the thread linked from your first post in the thread that were inserted by us.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: hmm

Post by RobertGonzalez »

adult.swim wrote:Would I be able to use:

Code: Select all

$fmlsnumber = $_GET['mlsnumber'];  (form input mlsnumber)

------and-------

# fetch the data from the database
while(odbc_fetch_row($result)){
  $mlsnumber = odbc_result($result, 2);   (database mlsnumber; the location of it in the database)

#  print("$mlsnumber \n");
}
to somehow compare the two?
First suggestion: try it. Then tell us what happened.
adult.swim
Forum Newbie
Posts: 9
Joined: Sun Jul 09, 2006 8:00 pm

solution

Post by adult.swim »

ok i just read the guide link that Feyd gave me for posting questions. I guess I really didn't give my question much thought, I'm going to rework it and then come back and ask a more intelligent and precise question. Thank for the help so far though.
Post Reply