[eluser]crispinatari[/eluser]
I've been trying to figure out why the rows in my table wont update for a few days and nothing is working yet, im trying to update/edit rows in a table
here is the error:
Quote:A Database Error Occurred
Error Number: 1062
Duplicate entry 'micjac' for key 1
UPDATE `tblemployee` SET `employeeID` = 'micjac' WHERE `employeeID` = 0
Quote:here is the form:
Edit Employee Details
Employee ID:
micjac
Department ID:
5551
First Name:
michael
Last Name:
jackson
click update button
here is the SQL
Code:
tblemployee CREATE TABLE `tblemployee` (
`employeeID` varchar(6) NOT NULL,
`departmentID` int(4) NOT NULL,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
PRIMARY KEY (`employeeID`),
KEY `FK_employeeID_departmentID` (`departmentID`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
here is the model and controller files and view
CONTROLLER
Code:
<?php
class Emps extends Controller {
function emps()
{
parent::Controller();
//$this->load->scaffolding('emps');
$this->load->model('empsmodel');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('html');
$this->load->library('validation');
$this->load->helper('form');
$this->load->scaffolding('entries');
}
function index()
{
$data['cssfile'] = base_url().'css/styles.css';
$data['cssfile2'] = base_url().'css/main.css';
$data['sidemenujs'] = base_url().'js/sliding_effect.js';
$data['title'] = "Employee application";
$data['heading'] = "Employee List";
//default method, gets data from model
//and lists all employees using default view
$data['res']=$this->empsmodel->get_all_emps();
$this->load->view('empslistview', $data);
}
function addemp()
{
$data['cssfile'] = base_url().'css/styles.css';
$data['cssfile2'] = base_url().'css/main.css';
//form for adding an entry';
$data['title'] = "Add Employee";
$data['heading'] = "Add Employee";
$this->load->view('empsaddview',$data);
}
function insertemp()
{
//form for adding an entry';
//calls the model function for actually inserting the new record
//get the data array from the post array sent by empsaddview.php
$data=$_POST;
print_r($data);
//if using helper
$data=$this->input->post();
//send the data to the insertemp function in the model
$this->empsmodel->insertemp($data);
//after insert, redirect back to main page
//header('Location: http://localhost/www/emps/index.php/emps/index');
//using url helper is easier, ie
redirect('empsaddview');
}
function deleteemp()
{
//called by empslistview url, with segment 3 = id
//which is still used by empsmodel->delete_entry
//id is not posted, just part of url in the link code
$this->empsmodel->deleteemp();
//using the url helper
redirect('views/index');
}
function editemp()
{
$this->load->database();
$this->db->where('employeeID',$this->uri->segment(3));
$data['query']=$this->db->get('tblemployee');
$this->load->view('empseditview',$data);
}
function update_emp()
{
$this->load->database();
$this->db->set('employeeID',$_POST['employeeID']);
$this->db->where('employeeID',$this->uri->segment(3));
$this->db->update('tblemployee');
//redirect('emps/editemp/');
redirect('emps/index/');
}
}
?>
MODEL
Code:
<?php
class Empsmodel extends Model {
function Empsmodel() {
parent::Model();
}
function get_all_emps()
{
$query = $this->db->get('tblemployee');
return $query->result();
}
function insertemp($data)
{
$this->employeeID = $data['employeeID'];
$this->departmentID = $data['departmentID'];
$this->first_name = $data['first_name'];
$this->last_name = $data['last_name'];
//$this->db->insert('emps', $this, $data);
$this->db->insert('tblemployee', $this, $data);
}
function deleteemp()
{
//the id is part of the url in the link in the empsview
//using sql in a query, but still using url helper
$this->db->query('delete from tblemployee where employeeID = "'.$this->uri->segment(3).'"');
$this->db->where('employeeID',1);
$this->db->delete('tblemployee');
//ActiveRecord makes this easier, ie
$this->db->delete('tblemployee', array('employeeID' => $this->uri->segment(3)));
}
}
?>