Jump to content

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


Recommended Posts

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 looks up 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 (but not necessarily understood!) some php 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):-

 

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

 

But then I just get really stumped.

 

I found this in one of the faq ares I visited:

 

$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 any kind person give me some guidance on this please?

 

Thanks

 

Anni

hello anni,

 

Well I guess you know how the receive data from a html form. Now you have to save the wanted form value into a variable

 

$html_radio = $_POST['radio_input'];

 

then you can construct the sql query and perform it:

 

$sql = "select email from tablename where email='".$html_radio." ' ";

$result =  $db->query($sql);

$num_rows = $db->query_num_rows($result);  //im guessing... just detect number of rows from result..

 

 

if( $num_rows ==0 ){

 

#  Email is not in the table..  :(  , so redirect him to a2,b2,c2...whatever

 

} elseif( $num_rows>0 ){

 

#  Email IS in the table. do something or redirect him to another page...

 

}

 

Hope this helps..

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.