How do you programatically add a user to a user entry field I have created a controller called myPlugin_Controller.php

This is the following code inside it.

 &lt;?php
class myPluginController extends BaseController
{
    public function actionSave()
    {

        $this-&gt;requirePostRequest();

        // get all the post variables
        $elementId = craft()-&gt;request-&gt;getPost('elementId');
        $userId = craft()-&gt;request-&gt;getPost('userId');

        // set the name of the field you want to update
        $fieldHandle = 'friends';

        // based on the element and field vars, get the models for each
        // using the elements and fields services
        $element = craft()-&gt;elements-&gt;getElementById($elementId);
        $field = craft()-&gt;fields-&gt;getFieldByHandle($fieldHandle);

        // store the current ids into a variable
        $currentIds = $element-&gt;{$field-&gt;handle}-&gt;ids();

        // merge the new id into the current ids
        $newIds = array_merge($currentIds, (array) $userId); 

        // save the new array of ids, using the relations service
        craft()-&gt;relations-&gt;saveRelations($field, $element, $newIds);

        // redirect to the redirect url
        $this-&gt;redirectToPostedUrl();

    }
}
?&gt;


This is my code in an html file, it is submitting some data however it returns a 404 error.

   &lt;form method="post" accept-charset="UTF-8"&gt; 
                  {{ getCsrfInput() }}
              &lt;input type="hidden" name="action" value="myPlugin/Save"&gt;
              &lt;input type="hidden" name="redirect" value="/success"&gt;

              &lt;input type="hidden" name="elementId" value="{{ user.id }}"&gt;

              &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;

              &lt;button type="submit" &gt;Submit&lt;/button&gt;
          &lt;/form&gt;


i'm not too sure where its going wrong now.

Also if your wondering why its called user this is why
        {% set user = craft.users.username( craft.request.segment(2) ).first() %}
Do you know where I am going wrong now any tips or help is all appreciated! :)

Still not working I have looked at craft cms documentation online i've followed your steps and there steps. 

Is there anything you can still see that is wrong?
You will need a custom plugin to do this.

I'll assume that you're familiar with that process. 

You can build a form in your template like this, passing in the entry ID you want to add the user to and the user ID you want to add.

&lt;form method="post" accept-charset="UTF-8"&gt; 
    {{ getCsrfInput() }}
    &lt;input type="hidden" name="action" value="myPlugin/save"&gt;
    &lt;input type="hidden" name="redirect" value="/success"&gt;

    &lt;input type="hidden" name="elementId" value="{{ entry.id }}"&gt;

    &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;

    &lt;button type="submit" &gt;Submit&lt;/button&gt;
&lt;/form&gt;


Then, you'll need a controller action in your plugin to which you submit a POST request from a form.

public function actionSave()
{
    $this-&gt;requirePostRequest();

    // get all the post variables
    $elementId = craft()-&gt;request-&gt;getPost('elementId');
    $userId = craft()-&gt;request-&gt;getPost('userId');

    // set the name of the field you want to update
    $fieldHandle = 'myUserField';

    // based on the element and field vars, get the models for each
    // using the elements and fields services
    $element = craft()-&gt;elements-&gt;getElementById($elementId);
    $field = craft()-&gt;fields-&gt;getFieldByHandle($fieldHandle);

    // store the current ids into a variable
    $currentIds = $element-&gt;{$field-&gt;handle}-&gt;ids();

    // merge the new id into the current ids
    $newIds = array_merge($currentIds, (array) $userId); 

    // save the new array of ids, using the relations service
    craft()-&gt;relations-&gt;saveRelations($field, $element, $newIds);

    // redirect to the redirect url
    $this-&gt;redirectToPostedUrl();

}


EDIT:

It's very important that you follow the naming conventions in the docs. 

You controller class should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed plugin and action names). 

Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.