questionable
questionable

Reputation: 37

Update Database tables with values input by user in CodeIgniter using PHP

I'm trying to update one of my user tables in the Database with values taken from the the user. But for some reason it's not updating anything.

HTML Form

<form class="form-horizontal" method = "post">
<fieldset>

    <!-- Form Name -->
    <legend>User Details</legend>

    <div>
        <?php echo $this->session->flashdata('msg'); ?>
    </div>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="name">Full Name</label>
        <div class="col-md-8">
            <input id="name" name="name" type="text" placeholder="something" class="form-control input-md">

        </div>
    </div>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="dob">Date of Birth</label>
        <div class="col-md-4">
            <input id="dob" name="dob" type="text" placeholder="" class="form-control input-md">

        </div>
    </div>

    <!-- Multiple Radios -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="gender">Gender</label>
        <div class="col-md-2">
            <select id="gender" name="gender" class="form-control">
                <option value="1">Male</option>
                <option value="2">Female</option>
            </select>
        </div>
    </div>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="degree">Degree</label>
        <div class="col-md-8">
            <input id="degree" name="degree" type="text" placeholder="degree" class="form-control input-md">

        </div>
    </div>

    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="specialization">Specialization</label>
        <div class="col-md-8">
            <input id="specialization" name="specialization" type="text" placeholder="specialization" class="form-control input-md">

        </div>
    </div>

    <!-- Select Basic -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="year">Degree Year</label>
        <div class="col-md-2">
            <select id="year" name="year" class="form-control">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>
    </div>

    <!-- Select Basic -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="semester">Semester</label>
        <div class="col-md-2">
            <select id="semester" name="semester" class="form-control">
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
        </div>
    </div>

    <!-- File Button -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="filebutton">Upload Profile Picture</label>
        <div class="col-md-4">
            <input id="filebutton" name="filebutton" class="input-file" type="file">
        </div>
    </div>

    <!-- Button -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="submit"></label>
        <div class="col-md-4">
            <a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
                <button id="submit" name="submit" class="btn btn-primary">Save Changes</button>
            </a>
        </div>
    </div>

</fieldset>

studentDashboardController

public function  saveUserDetails()
{
     $this->load->model('userModel');
     if (isset($this->session->userdata['logged_in']))
     {
          $username = ($this->session->userdata['logged_in']['username']);
     }
     $user = $this-> userModel-> getUserUid($username); //Gets the uid of the current user
     $this->userModel->saveUserDetails($user);
     $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Inserted Data</div>');
     $this->load->view('studentDashboard/common',$data);
     redirect(base_url('index.php/studentDashboard/editProfile',$data1));
}

userModel

public function saveUserDetails($uid)
{
    $data = array(
         'name' => $this->input->post('name')
    );
    $this->db->where("uid",$uid);
    $this->db->update("sysuser",$data);
}

The sysuser has the uid and name fields.I' not sure what I'm doing wrong here. Any help in this regard will be appreciated

Upvotes: 0

Views: 628

Answers (1)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

You dont send your post data to controller. Change $this->userModel->saveUserDetails($user); as

 $this->userModel->saveUserDetails($user,$this->input->post('name'));

Now Model should be like

public function saveUserDetails($uid,$name)
{
    $data = array(
         'name' => $name
    );
    $this->db->where("uid",$uid);
    $this->db->update("sysuser",$data);
}

and make sure you have set correctly form action like

 <form class="form-horizontal" method="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">

Upvotes: 1

Related Questions