1

I want to store the structure data in a mapping i.e. if I have a student structure as described below, then I should be able to have a myriad of students and distinguish between them using wallet addresses.

I want to store student data account wise and allow for retrieval of this data through wallet address.

I have written following code but I'm only able to retrieve the most recent setData() call of data.

contract FormSubmit{

  struct Student {
  string  email;
  string  password;
  string  address1;
  string  address2;
  string  city;
  string  state;
  string  zip;
  }

  mapping(address => Student) Students;

 function setData(address _address, string memory _email, string memory     _password,
string memory _address1, string memory _address2, string memory _city, string memory _state,
string memory _zip) public
{

  Students[_address].email = _email;
  Students[_address].password = _password;
  Students[_address].address1 = _address1;
  Students[_address].address2 = _address2;
  Students[_address].city = _city;
  Students[_address].state = _state;
  Students[_address].zip =_zip;
}

function getData(address _address) public view returns(string memory, string memory, string memory, string memory, string memory, string memory, string memory){
  return(Students[_address].email, 
  Students[_address].password,
  Students[_address].address1, 
  Students[_address].address2, 
  Students[_address].city, 
  Students[_address].state, 
  Students[_address].zip);
}}

Please Help me

EDIT

enter image description here

You can see in the getData I am getting information of last stored student not the previous student stored on this same address.

2 Answers 2

0

It works fine in my end.

On a serious note, If I call setData() with all function parameters filled with arguments and then call getData() then I'm able to distinguish between student data by storing student data under different wallet addresses.

It is, however, a horrible use case to add sensitive information like this to a public blockchain like ethereum. The student wallet might be protected by no public prostitution of their associated privatekey but with that being said, it seems like you'll be storing passwords and email addresses, which makes me think that this smart contract might be used in some sort of dApp which requires additional deep web access of some sort.

I would compare putting this on the Ethereum network, with running incredibly fast through a thorn bush ..while wearing no underwear (TLDR: I don't recommend).

4
  • Thanks for answering. Let me clarify It is for my test application as it is my first Dapp. I need to get all the students information stored on this address but I am getting the latest stored information on this address. That means I need to store the structure in array which I don't know how to store as I am new. can you please show me(or send me any sample link) how to set and get more then one student saved on one particular address. Jun 12, 2019 at 7:03
  • so you want to store information about multiple students under the SAME wallet address?for example let's take a scenario i.e. storing a classroom of students under one teacher's address? is it something like this that you want to accomplish?
    – 6egic
    Jun 12, 2019 at 11:07
  • yes, exactly. I want to store some students under one address and other students under second address. and return students structure for particular address(teacher) in getData. Jun 12, 2019 at 11:40
  • I see, check out this post ethereum.stackexchange.com/questions/54529/…, and more specifically, I think the comments to the answer might help with your question.
    – 6egic
    Jun 12, 2019 at 14:47
0

To get and set more than one student saved on one particular address using the following approach

contract FormSubmit{

  struct Student {
      string  email;
  }

  event SetData (
     uint studentId
  );

  mapping(address => Student[]) Students;

 function setData(address _address) public
{
  // this enables you to retreive a student detail
  // using the array index as id
  uint studentId = students.length;
  Student student = Student(_email);
  Students[_address].push(student);
  emit SetData(studentId); 
}

function getData(address _address, uint _studentId ) public view returns(string memory){
  return(Students[_address][_studentId].email);
}}
5
  • in getData if we want to get all the student for the particular address then what?Also event SetData { uint studentId; } gives error are you sure it's event ? Jun 12, 2019 at 10:24
  • It might be event SetData ( uint studentId ); Jun 12, 2019 at 10:30
  • Also you might forgot to add _email in setData parameters Jun 12, 2019 at 10:33
  • If we want to get all the student for the particular address You can do ``` contract.Students.call(_address) ```
    – Samparsky
    Jun 12, 2019 at 10:35
  • sorry but Not working Jun 12, 2019 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.