0

How do i write a query if the multiple form field data are being searched in mysql database.

The form fields are as follow

FirstName:
LastName:
Age:
Gender:

Now we can enter data in any multiple field and make a search the code i have tried works for single field. Unable to create for multiple conditions

sql = "select * from PERSON where F_Name = %s or L_Name = %s or Age = %s and Gender = %s" 
self.cursor.execute(sql,(self.fname, self.lname, self.age, self.gender))

If field FirstName and Gender are filled

If field Lastname, Gender, age are filled

If field FirsName, Age are filled

If field Gender and lastname are filled

like all possible conditions

5

1 Answer 1

0

This is not python or any language specific. This is about query syntax.

Try this

sql = "select * from PERSON where (F_Name = '%s' or L_Name = '%s' or Age = '%s') and Gender = '%s';" self.cursor.execute(sql % (self.fname, self.lname, self.age, self.gender))

Refer this http://www.tuxradar.com/practicalphp/9/3/13

7
  • do it fulfill all the condition ? i can enter data in any field and can make a search ? does it work that way ?
    – Jeremy
    Commented Nov 14, 2013 at 7:20
  • It doesn't work as expected. It satisfies only certain conditions
    – Jeremy
    Commented Nov 14, 2013 at 7:22
  • add ' before and after of each %s. Commented Nov 14, 2013 at 7:24
  • Your output query like this "select * from PERSON where (F_Name = 'first_name' or L_Name = 'last_name' or Age = '23') and Gender = 'male';" And make sure Age is string or integer. Commented Nov 14, 2013 at 7:26
  • Age is a string. And all my data are coming from the form, so its dynamic
    – Jeremy
    Commented Nov 14, 2013 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.