Page 1 of 1

comparing form to database

Posted: Sun Jul 09, 2006 8:02 pm
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]

Posted: Sun Jul 09, 2006 8:18 pm
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.

strings

Posted: Sun Jul 09, 2006 8:32 pm
by adult.swim
I know how to create strings, but I don't know how to use that in my query.

Posted: Sun Jul 09, 2006 8:35 pm
by dull1554
make your query something like this

Code: Select all

SELECT * FROM tablename WHERE fieldname = "fieldcontents"

Posted: Sun Jul 09, 2006 8:36 pm
by dull1554
then something like

Code: Select all

if($result['fieldname'] == $fieldcontents)
{
     //do wht you want to do
}

more over

Posted: Sun Jul 09, 2006 9:49 pm
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?

Posted: Sun Jul 09, 2006 9:53 pm
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
?>

Posted: Sun Jul 09, 2006 10:17 pm
by dull1554
what i posted was ment not as real code but just an example "frame" for a query and compairison. register_globals == VERYBAD :)

hmm

Posted: Sun Jul 09, 2006 10:20 pm
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?

Posted: Sun Jul 09, 2006 10:33 pm
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.

Re: hmm

Posted: Sun Jul 09, 2006 10:49 pm
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.

solution

Posted: Mon Jul 10, 2006 12:27 am
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.