如果不是全部运行,则从多个查询返回成功消息

时间:2016-05-26 23:08:43

标签: php

我在一个函数中有4个查询,这是nsert创建一个简历。这包括教育,就业,技能和加入他们的主要简历。

现在,当他们创建简历时,他们不会总是填写所有不同的表格来创建上述内容(教育,工作,技能),但我需要返回1条成功消息。我不能在rowcount上使用if else:因为这会返回所有这些或没有的值,所以我怎么能回应:

Alert::success('adding_resume', true);

即使某些表格中的表格空了,也会一次;

public static function create_resume()
{

    // Let's define the rules and filters
    $rules = array(
        'resume_title'    => 'max_len,100|required',
        'expected_level'    => 'max_len,100|required',
        'expected_salary'    => 'required|max_len,12|integer',
        'work_location'    => 'max_len,100',
        'year_experience'    => 'max_len,100|integer'
    );


    //validate the info
    $validated = FormValidation::is_valid($_POST, $rules);

    // Check if validation was successful
    if($validated !== TRUE):    

        //exit with an error
        exit(Alert::error(false, true, $validated));

    endif;

    //initate db
    $database = DatabaseFactory::getFactory()->getConnection();


    //sql to run
    $sql = "INSERT INTO resumes
            (
                resume_title,
                resume_work_level,
                resume_total_work_experience,
                resume_salary,
                resume_work_country,
                resume_about,
                resume_user,
                resume_date_created
            )
            VALUES
            (
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?
            )"; 

    //run the sql
    $resume = $database->prepare($sql);
    $resume->execute(array(Request::post('resume_title'), Request::post('expected_level'), Request::post('year_experience'), 
                        Request::post('expected_salary'), Request::post('work_location'), Request::post('about'), Session::get('id'),
                        date("Y-m-d")));


    //results?
    if($resume->rowCount())
    {

        //resume id
        $resume_id = $database->lastinsertid();

        //create employment
        $sql_employment = "INSERT INTO employment
                           (
                                employment_company,
                                employment_name,
                                employment_datestart,
                                employment_dateend,
                                employment_description,
                                employment_country,
                                employment_user,
                                employment_resume
                           )
                           VALUES
                           (
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?,
                                ?
                           )";

        //run the sql
        $employments = $database->prepare($sql_employment);

        //if they have more than 1
        foreach($_POST['experience'] as $employment): 
            $employments->execute(array($employment['company'], $employment['title'], date("Y-m-d", strtotime($employment['year_begin'])), 
                                  date("Y-m-d", strtotime($employment['year_finish'])), $employment['notes'], $employment['country'], 
                                  Session::get('id'), $resume_id));
        endforeach; 

        if($employments->rowCount())
        {

            //sql to run
            $education_sql = "INSERT INTO education
                              (
                                    education_institute,
                                    education_degree,
                                    education_dateobtained,
                                    education_dateexpires,
                                    education_description,
                                    education_user,
                                    education_date,
                                    education_resume

                              )
                              VALUES
                              (
                                    ?,
                                    ?,
                                    ?,
                                    ?,
                                    ?,
                                    ?,
                                    ?,
                                    ?
                              )";

            //run the sql
            $educations = $database->prepare($education_sql);

            //for multiple education
            foreach(Request::post('education') as $education):
                $educations->execute(array($education['institution'], $education['degree'], date("Y-m-d", strtotime($education['year_begin'])), 
                                    date("Y-m-d", strtotime($education['year_finish'])), $education['about'], Session::get('id'), date("Y-m-d"), 
                                    $resume_id));   

            endforeach;

            //results
            if($educations->rowCount())
            {
                //sql to run
                $sql_skill = "INSERT INTO skills
                              (  
                                   skill_type,
                                   skill_percent,
                                   skill_user,
                                   skill_resume
                              )
                              VALUES
                              (
                                    ?,
                                    ?,
                                    ?,
                                    ?
                              )";
                //run the sql
                $skills = $database->prepare($sql_skill);

                //for each skill
                foreach(Request::post('skill') as $skill):
                    $skills->execute(array($skill['name'], $skill['percentage'], Session::get('id'), $resume_id));
                endforeach;

                //the results?
                if($skills->rowCount())
                {
                    Alert::success('adding_resume', true);
                }else{
                    Alert::alert('adding_resume', true);
                }
            }else{
                Alert::error('adding_resume', true);
            }

        }else{
            Alert::error('adding_resume', true);
        }

    }else{
        Alert::error('adding_resume', true);
    }

}

1 个答案:

答案 0 :(得分:0)

这对你来说似乎是一种奇怪的方式,只是尝试插入所有内容而不检查任何内容,但你可以这样做:

  1. 创建一个最初设置为false的布尔值。
  2. 在每次查询后,检查结果。
  3. 如果结果为true,则将布尔变量更改为true。如果结果为false,请不要更改布尔变量。

  4. 运行所有查询后,如果您的布尔变量为true,则至少有一个查询成功。

  5. 示例代码:

    $good_query = false;
    
    <build sql query for resume stuff>
    
    $check = $resume->execute(<blah>);
    
    if ($check) { $good_query = true; }
    
    <build sql query for employment stuff>
    
    $check = $resume->execute(<blah>);
    
    if ($check) { $good_query = true; }
    
    <build sql query for education stuff>
    
    $check = $resume->execute(<blah>);
    
    if ($check) { $good_query = true; }
    
    <build sql query for skills stuff>
    
    $check = $resume->execute(<blah>);
    
    if ($check) { $good_query = true; }
    

    如果$ good_query在结尾处为真,则至少有一个查询成功。