1

Can anyone school me on how to have MySQL via Codeigniter syntax average form data and insert it into the corresponding database table upon submission?

I am able to collect the form information and store it in the database correctly. I just don't know the general coding for averaging the data and storing it in the appropriate column of table.

For instance: If the user enters five test scores: 98 99 46 93 and 55 and presses "Submit" each item is successfully inserted in its correct column in the table ('test1', 'test2', 'test3', 'test4', and 'test5'). However, I would like the sixth column to be the average of the scores (i.e. 'test_avg'). But, I don't know how to code that part of the process.

4

4 回答 4

3

You should not store that calculated value in you table. Instead you shoud use a view on top of that table that performs the calculation for you.

于 2013-09-09T15:29:51.390 回答
1

you can simply use php for that

$avg=(98+99+46+93+55)/5;

and make an insert or update query.

于 2013-09-09T15:29:52.720 回答
0

Why not simply

(test1+test2+..+test5)/5 as test_average
于 2013-09-09T15:29:17.460 回答
0

CodeIgniter has it's own syntax for forms (See http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html as a reference), but in the end, it doesn't matter too much how you structure your SQL database. You will still need a controller that does the actual submission for you. At this point, you can add other data that you want to insert into the database.

I think you are already on the right track of having one column per field. For the average, calculate it yourself before you submit the form and insert it into the proper column.

$average = ($test1 + $test2 + $test3 + $test4 + $test5) / 5;
于 2013-09-09T15:33:57.977 回答