Welcome Guest, Not a member yet? Register   Sign In
Insert computed value into SQL database
#1

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

Attached Files Thumbnail(s)
   
Reply
#2

(This post was last modified: 01-02-2017, 07:42 AM by neuron.)

it seems like your problem is in type of percentage column, in mysql you should define it as decimal(5,2)
Reply
#3

(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.
Reply
#4

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.
Reply
#5

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB