Keep record in mapping (no over-writing)

17 Views Asked by At

contract YourContract {
    struct student_marks
    {
        uint maths ;
        uint science ;
    }

    mapping (address => student_marks) public record ;

    function add_marks(uint _maths, uint _science) public
    {
        record[msg.sender] = student_marks(_maths , _science) ;
    }
}

If msg.sender updates his marks the previous marks will be overwrite. Please help me how to keep record of marks of every msg.sender. No over-written. Drop a comment please if you think there's a solution. Hope so I elaborate my problem in a right way. Thankyou !

To save the key-value data of mapping without over-write the previous entries

1

There are 1 best solutions below

0
Rafael Zerbini On

Well, you need an array of records, not a single one.

mapping(address => StudentMarks[]) public records;

Then for your addMarks function

StudentMarks memory newMarks = StudentMarks(_maths, _science);

records[msg.sender].push(newMarks);

Then you just need to build your getMarks strategy, you can do it by index for example.