Welcome Guest, Not a member yet? Register   Sign In
Why my empty array still inserts into the my db table?
#1

[eluser]towki[/eluser]
Okay so i have this function in my model

Code:
public function add($offense,$weapons,$transpo) {

$data1 = array(
'report_no' => $this->input->post('report_no'),
'reporting_unit' => $this->input->post('reporting_unit'),
'offenses_committed' => $offense,
'comission_dateAndTime' => $this->input->post('comission_dateAndTime'),
'commission_Place' => $this->input->post('commission_Place'),
'case_status' => $this->input->post('case_status'),
'weapons_used' => $weapons,
'transport_used' => $transpo,
'narrative'=>$this->input->post('narrative')
);

if (!empty($data1)){

return $this->db->insert('crime_details', $data1);

}

}

When $data1 array is empty,why it still perform inserts into my database table,so a new row will be added and only the id field which is auto incrementing has a value, and the rest is blank. I dont want this to happen,.. what i want is ,when there is no data to be inserted, then dont perform insert, dont add new empty rows in my table. Sorry for my english
#2

[eluser]Aken[/eluser]
Because you're defining your array with a key => value structure, the array is not considered empty. You'll need to go through and check the values of $data1 in order to see if it's "empty".
#3

[eluser]towki[/eluser]
[quote author="Aken" date="1352441721"]Because you're defining your array with a key => value structure, the array is not considered empty. You'll need to go through and check the values of $data1 in order to see if it's "empty".[/quote]

I only got this way of inserting data in the user guide.Could you tell me the other way to define my array? So when the array is empty will be considered empty.
#4

[eluser]xeroblast[/eluser]
try:
Code:
if ($this->input->post('report_no')) $data['report_no']=$this->input->post('report_no');
if ($this->input->post('reporting_unit')) $data['reporting_unit']=$this->input->post('reporting_unit');
if ($offense) $data['offenses_committed']=$offense;
#5

[eluser]towki[/eluser]
Thanks XeroBlast! problem solved for now.




Theme © iAndrew 2016 - Forum software by © MyBB