根据用户输入MySQL创建SQL语句

what I want to do is this:

First have a form that has 5 input for example:

Name: 
Career: 
Experience:
City:
Speciality:

what I need to do is create a sql sentence according to the fields that are filled. If only 2 field were filled create a sql with only those fields. I don't have any idea how to do this besides creating a sql sentence for every possibility which turns out to be a pretty extensive work. i read that i could use store procedures, but again I don't know how I would do it. please help me!


well sorry if i didn't elaborate the question.

here is the deal i have diferent tables in mysql database

  • person
    • person_id
    • name
    • last_name
  • person_courses
    • person_id
    • course
  • person_carrer
    • person_id
    • carrer
  • person_experience
    • person_id
    • position
    • experience_description
    • experience_from_date
    • experience_to_date

all this tables are related to person by foreign_key (person_id)

then i have a page where user have 6 diferent inputs:

  • name:
  • last_name:
  • courses:
  • experience:(this fields search position and experience_description from table person_experience)
  • antiquity:(must obtain a sumatory of all person_experience)

the user must obtain results according to the fields he filled.

what i tried to do is create a complex SQL sentence using "%%" like this

select a.person_id, b.name, c.last_name, d.courses, f.experience, g.antiquity
from person a,
    (
     'SQL SENTENCE' WHERE NAME LIKE "%%"
    ) b,
    (
     'SQL SENTENCE' WHERE LAST_NAME LIKE "%%"
    ) c,
    (
     'SQL SENTENCE' WHERE COURSE LIKE "%%" GROUP BY PERSON_ID
    ) d, 
    (
     'SQL SENTENCE' WHERE POSITION LIKE "%%" OR EXPERIENCE_DESCRIPTION LIKE "%%" 
      GROUP BY PERSON_ID
    ) f,
    ( 
     'SQL SENTECE' 
     WHERE 'SUMATORY OF ALL PERSON EXPERIENCE, (TO_DATE - FROM_DATE)/365 '>=0 (THIS ZERO WOULD CHANGE IF USER FILLS INPUT ANTIQUITY)
     GROUP BY PERSON_ID
    ) g
    WHERE a.person_id = b.person_id and c.person_id = b.person_id and d.person_id = c.person_id and f.person_id = d.person_id and g.person_id = f.person_id

and if the user fills any field i just put the value between '%value%' so this way i get the result, and i get results with this sql sentence but the problem is that if a person doesn't have a experience or carrer it is not shown in the result, so i need someway, as i said in the firstplace, just search in the database according to the users filled inputs.

for example in if the user fills:

  • carrer, experience: search all person with that carrer and that experience.
  • name: search all person even if they dont have carrer or course or experience with that name

PS: trying to create an sql sentece for every posibility would result in 64 sql senteces and i am not thinking about doing it!

THANK FOR ALL THE HELP IN ADVANCE

Here is something that may help:

$sql = array(); // initialize empty array for data

foreach(array("name", "career", "experience", "city", "speciality") as $key) { // iterate all the form controls
    if(!empty($value = trim($_POST[$key]))) {
        // the form input is filled, so we add it to data array (with proper escaping)
        $sql["`".$key."`"] = "'".mysqli_real_escape_string($value)."'";
    }
}

if(count($sql)) { // only if user had provided some data
    // generate the final SQL statement
    $sql = "INSERT INTO `table` ("
        .implode(", ", array_keys($sql)).") VALUES (" // for example (`name`, `career`)
        .implode(", ", array_values($sql)) . ")"; // for example ('Foo', 'Bar')
} else {
    // no informations provided
}