Page 1 of 1

Query db from form input. If match then go to page...

Posted: Fri Jan 25, 2008 4:47 am
by annii
Hello all,
I really do hope that this is not too much of a newbie question.

I have a MySQL database set up. Within it, I have created a table with the following fields:
[id][email][name][jobtitle][company][phone][country]

I want to create a short form that looksup the email address, checks if it's in the database, if it is, (and based on radio button a b or c selection) then it will send the user to page a, b or c respectively. If it is not, then it would send the user to page a2, b2 or c2 respectively.
(Now, creating the form I can do!)
My problem is how to write the query.

I have read the faq for newbies (and performed numerous google searches).
I can work out how to connect to the database.
I can see that I need some kind of select statement like this (but I know the end bit is wrong as email needs to equal form input value and I'm not sure how to say that):-

Code: Select all

$result= $xxx->db->query('SELECT email FROM tablename WHERE email= '?');
But then I just get really stumped.

I found this in a faq section:

Code: Select all

$a = “b”; 
if($a == “a”){ 
  //do something 
}elseif($a == “c”){ 
  //do something 
}else{ 
  echo “$a isn’t a or c”; 
}
I can see it might help form the basis of what I want to do, but I just don't have the knowledge to build on it yet.

Can anykind person give me some guidance on this please?

Thanks

Anni

Re: Query db from form input. If match then go to page...

Posted: Fri Jan 25, 2008 6:14 am
by aceconcepts
annii wrote:

Code: Select all

$result= $xxx->db->query('SELECT email FROM tablename WHERE email= '?');
Hello,

What you need to do is create a variable i.e. $strEmail and assign the posted email value to it:

Code: Select all

$strEmail=$_POST['email'];   //THE $_POST['email'] will be the name of your email form field
Now what you do is insert this email variable into your query:

Code: Select all

$result= mysql_query("SELECT email FROM tablename WHERE email= '$strEmail'");
If you get stuck just reply with your problem :)

Re: Query db from form input. If match then go to page...

Posted: Fri Jan 25, 2008 7:16 am
by annii
Hi there, thanks so much for your reply, so now I have the first bit:

Code: Select all

$strEmail=$_POST['f2_email'];   
$result= mysql_query("SELECT email FROM tablename WHERE email= '$strEmail'");
That makes sense, so, as a newbie, this is effectively saying:
$strEmail is the posted form field value of f2_email,
Check the database table for an email field that matches $strEmail.
Right?

So then I guess I have to do something
that uses logic like:-
if result then check radio option value
if a then form action = go to url a,
or
if b then form action = go to url b,
or
if c then form action = go to url c,

else, if no result, then check radio option value

if a then form action = go to url 2a,
or
if b then form action = go to url 2b,
or
if c then form action = go to url 2c,

So now you can see just how little I understand about PHP, I mean I can look at it and see what it's doing (in general) but I'm finding it really hard to think what I need it to do and translate it into php code.

From the code you posted above, I suppose I have to identify the post data from the radio buttons in a similar way, would it be something like:

Code: Select all

$urlselecta=$_POST['urlselecta'];
if($urlselect=="urlselecta")
I am so bad at this, I hate having zilch knowledge on php. I've booked myself on a basic course next month, but in the meantime, any extra help with this would be much appreciated.

Thanks

Anni

Re: Query db from form input. If match then go to page...

Posted: Fri Jan 25, 2008 8:20 am
by aceconcepts
Hi Anni,

You're getting the hang of it. At first it is really quite confusing but remeber that you're trying to tell a computer to do something through code - not exactly easy if you're new.

So, you are correct about:
1. $strEmail=$_POST['f2_email'];
2. $result= mysql_query("SELECT email FROM tablename WHERE email= '$strEmail'");
In order to get a value from a radio button you must have initialised a value to pass from the corresponding radio button.

The syntax/code for a typical radio button looks something like this:

Code: Select all

<input type="radio" name="myRadio" value="Hello this is my value" />
So, When you "GET" this "POSTED" radio button's value you will be getting the following: "Hello this is my value". And yes you're correct in how to get the radio button's value - well done :)

You have hit the nail on the head by saying "the rest is logic". There is so much logic involved in programming and this is probably the most difficult part.