I m trying to insert some data into a table but I want to only insert if the variable is not empty.
My model code below.
$query_str = "INSERT INTO todo (";
$query_and=array();
if( !empty($one_input)){
$query_and[]= 'one';
}
if( !empty($two_input)){
$query_and[]= 'two';
}
if( !empty($three_input)){
$query_and[]= 'three';
}
$query_str .= implode(', ', $query_and);
$query_str .= ') VALUES (?, ?, ?)';
$query_data=array();
if( !empty($one_input)){
$query_data[]= $one_input;
}
if( !empty($two_input)){
$query_data[]= $two_input;
}
if( !empty($three_input)){
$query_data[]= $three_input;
}
$query_dat = implode(', ', $query_data);
$query_dat .= ");";
$q = $this->db->query($query_str, $query_dat);
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
INSERT INTO todo (one, two, three) VALUES ('val1, val2, val3);',