Welcome Guest, Not a member yet? Register   Sign In
Cannot update database record using posted variables
#1

[eluser]ccalby[/eluser]
Hello,

I'm pulling my hair out at the moment.
I've been using CI for about a month and cannot figure this out.

My intention is to have a URL like this
http://domain.com/cmc2/index.php/main/update/<GAMERTAG>
(where <GAMERTAG> is a manually entered string.

I can load the page find, but I cannot update it as I intend.
I have a form setup on the update page that gets data from the database based on the URL.
When the user clicks "submit", the data should then be updated in the database.

Error message
Quote:A Database Error Occurred

Error Number: 1062

Duplicate entry 'bookfgfdg' for key 'PRIMARY'

UPDATE `cmc_officers` SET `Gamertag` = 'bookfgfdg' WHERE `Gamertag` = 0

Filename: /usr/share/lcrp/cmc2/models/site.php

Line Number: 38
Simply put, the new Gamertag should be from the form that the user submits, the WHERE gamertag is from the URL (above).

Model:
Code:
function update_record($gamertagold) {
$data = array(
   'Gamertag' => $this->input->post('gamertag'),
  );
  
  $this->db->where('Gamertag', $gamertagold);
  $this->db->update('cmc_officers', $data);
}
Controller
Code:
function update()
{
  
    if($this->session->userdata('logged_in'))
    {
   $session_data = $this->session->userdata('logged_in');
   $data['username'] = $session_data['username'];
   $data['title'] = "CMC - Update Officer";
   $this->load->view('header', $data);
   $this->form_validation->set_rules('gamertag', 'Gamertag', 'required');
  
   if($this->form_validation->run() == TRUE)
    {
     $gamertagold = $this->uri->segment(3);
     $this->site->update_record($gamertagold);
     $this->session->set_flashdata('message', 'Successfully updated!');
    }
   elseif ($this->uri->segment(3) === FALSE)
   {
    show_error('A gamertag must be submitted!');
   }
  
  elseif($this->uri->segment(3) != FALSE)
  {
   $data['record'] = $this->site->get_record();
   $this->load->view('update', $data);

  }
  
  
  
    }
    
    else
    {
   //If no session, redirect to login page
   redirect('login', 'refresh');
    }
}

Thanks Smile
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Where are you setting $gamertagold in your controller? It doesn't look like it's being passed to the model method. Also, I would suggest you do a little more validation. What happens if a user picks a gamer tag that's already in use by someone else?

Check your table. You'll see that there's an entry in there already where Gamertag is 'bookfgfdg' and Gamertag is '0'.




Theme © iAndrew 2016 - Forum software by © MyBB