你想做的事是这样的(我认为):
$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')");
这里是一个查询回声演示:http://codepad.org/3NgGu1Tm
OR
作出新的查询每个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')");
}
或连接查询:
$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;
}
这里是假冒伪劣值的演示:http://codepad.org/ox4kG43b
@Neal:thanks :)但我想结果存储在每一行,而不是一行。 – tonoslfx 2011-04-21 14:29:18
@boyee比为什么把它存入数组? – Neal 2011-04-21 14:30:55
@Neal:也许你有另一种解决方案,请分享:) – tonoslfx 2011-04-21 14:32:15