CRUD – Update

Next up, we’re going to perform entry updates. As its name suggests, the update operation in CRUD performs an update or edit to existing entries in our players' mapping storage. Our update method will be editPlayer() , which enables the players themselves to edit their own details by sending a transaction to the Cplayer contract. Here’s the code for editPlayer() and the logic for editing individual contacts:

function editPlayer(string _name, uint256 _phonenumber, address _address,uint256 _id) public returns (bool) {
players[msg.sender].name = _name;
players[msg.sender].PhoneNumber = _phonenumber;
players[msg.sender].Paddress = _address;
players[msg.sender].id = _id;
return true;
}

As every user has their details mapped to their address (ID), we need only to target the ID in the mapping and update the attribute of the corresponding struct.

Again, let's make a point about immutability in Ethereum.

Every time you update a new entry, you don’t erase or override the old value. When you update a contract's state, you define a new value that will be included in the current block (on the blockchain), but the old value will still be living in a previous block. This is similar to Lavoisier's law: nothing is lost, everything is transformed.

In SQL, we can do the same thing using an UPDATE statement on the players, table based on the primary key for a record specified in the WHERE clause:

UPDATE players
SET name = name,...
WHERE ID = msg.sender;

However, unlike in SQL, you can’t alter an existing mapping as you do for tables. The code is definitively immutable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset