简体   繁体   中英

Inserting values from an array into a SQL query

how do i get the array value from below code?. and use while loop to save the data those are not empty. not quite sure what needs to be added from the code.

if(!empty($bookslot['t_deep'])) :
  $tw_deep = 'deep_wrap';
elseif(!empty($bookslot['t_eyelashes'])) :
  $tw_eyelashes = 'eyeLashes_wrap';
elseif(!empty($bookslot['t_holistic'])) :
  $tw_holistic = 'holistic_wrap';
elseif(!empty($bookslot['t_male_nail'])) :
  $tw_male_nail = 'male_nail_wrap';
endif; 

$arr[]  = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail);

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) VALUES('$arr', '1','$id_bookslot', '$b_ref')");

in the treatment_group, the array returns Array instead of 'deep_wrap, etch..

expecting for database :

    Array          1   123  abc // <-- result above code
--------------------------------------------------------
    deep_wrap      1   123  abc
    holistic_wrap  2   123  abc

what you want to do is this (i think):

$arr  = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail);
$arrPlode = implode(', ', $arr);

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref)
                  VALUES('$arrPlode', '1','$id_bookslot', '$b_ref')");

here is a demo with a query echo: http://codepad.org/3NgGu1Tm

OR

make a new query for each if:

if(!empty($bookslot['t_deep'])) {
  $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
      VALUES('deep_wrap', '1','$id_bookslot', '$b_ref')");
}
if(!empty($bookslot['t_eyelashes'])) {
  $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
      VALUES('eyeLashes_wrap', '1','$id_bookslot', '$b_ref')");
}
if(!empty($bookslot['t_holistic'])) {
  $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
      VALUES('holistic_wrap', '1','$id_bookslot', '$b_ref')");
}
if(!empty($bookslot['t_male_nail'])) {
  $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
      VALUES('male_nail_wrap', '1','$id_bookslot', '$b_ref')");
}

OR CONCATENATE THE QUERIES:

$concat = array();

if(!empty($bookslot['t_deep'])) {
  $concat[] = "('deep_wrap', '1','$id_bookslot', '$b_ref')";
}
if(!empty($bookslot['t_eyelashes'])) {
  $concat[] = "('eyeLashes_wrap', '1','$id_bookslot', '$b_ref')";
}
if(!empty($bookslot['t_holistic'])) {
  $concat[] = "('holistic_wrap', '1','$id_bookslot', '$b_ref')";
}
if(!empty($bookslot['t_male_nail'])) {
  $concat[] = "('male_nail_wrap', '1','$id_bookslot', '$b_ref')";
}

$db->query(build_insert_query('checked_treatments', 
         'treatment_group, id_treatment, id_booking, b_ref', $concat));


function build_insert_query($table, $cols, $values){
        $return = "INSERT INTO $table ($cols) VALUES";
        $val_length = count($values);
        foreach($values as $key=>$val){
             $return .= $val;
             if($key < ($val_length-1)){ $return .= ", ";  }
        }
        return $return;
}

here is a demo of the above with fake values: http://codepad.org/ox4kG43b

drop the square brackets on the declaration to be. You are making a 3 dimensional array otherwise

$arr  = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail);

And in your query you still need to dereference the array to get the value by specifying the index

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) VALUES('$arr[0]', '1','$id_bookslot', '$b_ref')");

An index of 0 will return the value of $tw_deep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM