PHP插入mysql(wampserver)没有错误,但仍然无法正常工作

时间:2020-02-14 12:44:48

标签: php mysql wampserver

您好,我尝试使用PHP7和wampserver插入数据库中的表中,但是即使错误代码为00000并且调试了所有变量并且它们具有正确的值,它也无法正常工作,我只得到了插入为了使表“餐”正常工作,这是我编写的代码,任何提示都可以帮助您,谢谢。

<?php

function setConsultation($datas, $questionnaire, $nomeal, $type, $payement) 
{

    global $db;

    // Build sql syntax
    // for `Health` and `Questionnaire`
    $sql['health'] = '';

    foreach($questionnaire as $k=>$v) {
        $sql['health'] .=
           "INSERT INTO health(id_quest,qu_value)
            VALUES (:id_quest{$k},:qu_value{$k} );

            -- Questionnaire
            INSERT INTO questionnaire(id_health,id_cons)
            VALUES (LAST_INSERT_ID(),@cons_id);";
    }

    // Conditional meal
    $sql['meal'] = array('sql'   => '','value' => '');

    if (!$nomeal) {

        $sql['meal']['sql'] =
           'INSERT INTO meal(id_exp,quantity,price,ordered)
            VALUES(1,1,0,NOW());

            SET @meal_id = LAST_INSERT_ID();';

        $sql['meal']['value'] = '@meal_id';

    }else
        $sql['meal']['value'] = 'NULL';


    $sql['payement'] = $payement ?: '(SELECT MAX(id_price) FROM price)';


    // Prepare request
    $q = $db->prepare(

       /*-- Meal */
       $sql['meal']['sql'].

       /* -- Consultation */
       "INSERT INTO consultation(id_type,id_meal,id_price,height,weight,
                                fat,water,muscle,perimeter,goal,level,
                                program_visibility,date_start)
        VALUES (:type, {$sql['meal']['value']}, {$sql['payement']},:height,:weight,
            (CASE :fat       WHEN '' THEN null ELSE :fat        END),
            (CASE :water     WHEN '' THEN null ELSE :water      END),
            (CASE :muscle    WHEN '' THEN null ELSE :muscle     END),
            (CASE :perimeter WHEN '' THEN null ELSE :perimeter  END),
            :goal,:level,:visibility,NOW()
        );

        SET @cons_id = LAST_INSERT_ID();

        -- Exam
        INSERT INTO exam(id_cons,id_user)
                VALUES(@cons_id,:id_user);

        -- Health
        ".$sql['health']

    );


    // Consultation
    $q->bindValue(':type'       , (int)$type                        , PDO::PARAM_INT);
    $q->bindValue(':height'     , (int)$datas['height']             , PDO::PARAM_INT);
    $q->bindValue(':weight'     , number_format($datas['weight'], 1)                );
    $q->bindValue(':fat'        , (int)$datas['fat']                , PDO::PARAM_INT);
    $q->bindValue(':water'      , (int)$datas['water']              , PDO::PARAM_INT);
    $q->bindValue(':muscle'     , (int)$datas['muscle']             , PDO::PARAM_INT);
    $q->bindValue(':perimeter'  , (int)$datas['perimeter']          , PDO::PARAM_INT);
    $q->bindValue(':goal'       , number_format($datas['goal'], 1)                  );
    $q->bindValue(':level'      , (int)$datas['level']              , PDO::PARAM_INT);
    $q->bindValue(':visibility' , call_user_func(function(){
        $main = json_decode(file_get_contents(BASE.'params/main.json'), true);
        return (int)$main['program']['visibility'];
    }), PDO::PARAM_INT);

    // Exam
    $q->bindValue(':id_user'    , (int)$_SESSION['data_user']['id'] , PDO::PARAM_INT);

    // Health
    foreach($questionnaire as $k=>$v) {

        $q->bindValue(":id_quest{$k}", (int)$k                      , PDO::PARAM_INT);
        $q->bindValue(":qu_value{$k}", $v                           , PDO::PARAM_BOOL);
    }

    $q->execute();    

  //  die($sql->errorCode());
    return $q->errorCode().'-'.$db->errorCode();

}

0 个答案:

没有答案