0

Hello I am trying to insert into tables located in a database using PHP7 and wampserver but it is not working even though the error code is 00000 and I debugged all the variables and they have the correct values, I am getting only the insert for table 'meal' working properly, here is the code I wrote, any tip could help, thanks.

<?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();

}
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
MR Skan
  • 97
  • 8

0 Answers0