Insert computed value into SQL database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Insert computed value into SQL database (/showthread.php?tid=67012) |
Insert computed value into SQL database - cndunga - 01-02-2017 I would like to insert the following computed value $this->data['score_percentage'] = 0; if ($score != 0) { $this->data['score_percentage']=number_format( (($score/$totalQuestions)*100),2 ); } if (isset($useroptions) && count($useroptions)>0) { $userid = $this->session->userdata('user_id'); unset($where); unset($records); $data['userid'] = $userid; $data['email'] = $this->session->userdata('email'); $data['username'] = $this->session->userdata('username'); $data['quiz_id'] = $quizinfo->quizid; $data['score'] = $score; $data['total_questions'] = $totalQuestions; $data['dateoftest'] = date('y-m-d'); $data['timeoftest'] = date('H:i'); into the the attached table format RE: Insert computed value into SQL database - neuron - 01-02-2017 it seems like your problem is in type of percentage column, in mysql you should define it as decimal(5,2) RE: Insert computed value into SQL database - cndunga - 01-02-2017 (01-02-2017, 07:41 AM)neuron Wrote: it seems like your problem is in type of percentage column, in mysql you should define it as decimal(5,2) Dear Neuron, Thanks for the quick reply. The problem is actually that the script code is computing the percentage correctly. My problem is inserting the computed value for score_percentage into the percentage column in the database table user_quiz_results. RE: Insert computed value into SQL database - PaulD - 01-02-2017 As Neuron pointed out, your database table has a percentage column that looks like an integer data type. Your percentage is being calculated to two decimal places. So to insert a decimal into the database you need to define that column as a decimal data type, not an integer. Integers (int) values are whole numbers. Databases distinguish between integers and decimals, and a decimal cannot be an integer. Even 2.0 is not an integer. It is a decimal to one decimal place. Follow Neurons advice and change your column format in the database and see if you get any errors now. RE: Insert computed value into SQL database - cndunga - 01-02-2017 (01-02-2017, 12:41 PM)PaulD Wrote: As Neuron pointed out, your database table has a percentage column that looks like an integer data type. Your percentage is being calculated to two decimal places. So to insert a decimal into the database you need to define that column as a decimal data type, not an integer. Integers (int) values are whole numbers. Databases distinguish between integers and decimals, and a decimal cannot be an integer. Even 2.0 is not an integer. It is a decimal to one decimal place. Follow Neurons advice and change your column format in the database and see if you get any errors now. Thanks Paul for the explanation. I will make the change |