[eluser]riwakawd[/eluser]
On my form, when I click on my submit button what ever one I update then it changes all the database table rows to what has just been updated.
I am trying to convert my old code to way codeigniter way but my old way seems to be the only way for it to work WHERE INSERT INTO Rather than update. Which I prefer.
Can not seem to get new way to preform same as old way.
Code: <?php
class Model_setting extends CI_Model {
NEW WAY
public function editSetting($group, $data, $website_id = 0) {
foreach ($data as $key => $value) {
if (!is_array($value)) {
$this->db->set(array('website_id' => $website_id, 'group' => $group, 'key' => $key, 'value' => $value));
$this->db->update('setting');
} else {
$this->db->set(array('website_id' => $website_id, 'group' => $group, 'key' => $key, 'value' => serialize($value), 'serialized' => '1'));
$this->db->update('setting');
}
}
}
OLD WAY // WORKS
public function editSetting1($group, $data, $website_id = 0) {
$this->db->query("DELETE FROM " . $this->db->dbprefix . "setting WHERE website_id = '" . (int)$website_id . "' AND `group` = " . $this->db->escape($group) . " ");
foreach ($data as $key => $value) {
if (!is_array($value)) {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "setting SET or WHERE
`website_id` = '" . (int)$website_id . "',
`group` = " . $this->db->escape($group) . ",
`key` = " . $this->db->escape($key) . ",
`value` = " . $this->db->escape($value) . "
");
} else {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "setting SET or WHERE
`website_id` = '" . (int)$website_id . "',
`group` = " . $this->db->escape($group) . ",
`key` = " . $this->db->escape($key) . ",
`value` = " . $this->db->escape(serialize($value)) . ",
serialized = '1'
");
}
}
}
}
Controller
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index () {
$this->load->model('admin/setting/model_setting');
// Temp Input server will use form validation later.
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$this->model_setting->editSetting('config', $this->input->post());
redirect('admin/dashboard');
}
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
if (trim($this->input->post('config_meta_title'))) {
$data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if (trim($this->input->post('config_maintenance'))) {
$data['config_maintenance'] = $this->input->post('config_maintenance');
} else {
$data['config_maintenance'] = $this->configs->get('config_maintenance');
}
return $this->load->view('setting/settings', $data);
}
}
[eluser]Tim Brownlaw[/eluser]
If you want to update a row, you need to tell it which row or rows you want to update.
What you have told it is to update every row.
So you need to define a WHERE before you perform the update.
By leaving that out - it's assuming WHERE is EveryWhere
So you need to know what fields ( columns ) in your table uniquely identify the row (or rows) you want to update?
As a side note: I am curious as to how your "old way" works. Something I'll have to look into later...
[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1410566567"]If you want to update a row, you need to tell it which row or rows you want to update.
What you have told it is to update every row.
So you need to define a WHERE before you perform the update.
By leaving that out - it's assuming WHERE is EveryWhere
So you need to know what fields ( columns ) in your table uniquely identify the row (or rows) you want to update?
As a side note: I am curious as to how your "old way" works. Something I'll have to look into later...[/quote]
OK So that means Manually tell it which have to much code for that. Will work on it then.
[eluser]Tim Brownlaw[/eluser]
Well yes...
As an example, something like
Code: $this->db->where('website_id',$website_id);
I have no idea of your table structure and what constitutes your 'key'.
I see that you are actually updating 'website_id' which you wouldn't do if it was an update based on that field.
So change it to suit.
Remember that the user guide is your friend!
[eluser]Tim Brownlaw[/eluser]
And as a part of your debugging/validation you can use this after any DB action ie in this case immediately after your $this->db->update();
Code: echo $this->db->last_query();
So you would have...
Code: $this->db->update('setting');
echo $this->db->last_query();
And if you need to stop the code running to see it - you can add an exit(); after it... Remember that is ONLY for Debug to check the SQL being created is what you expect and you can copy it and use it in phpmyadmin or mysql workbench and test it out.
[eluser]riwakawd[/eluser]
I have got it all working now
Controller
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index() {
$data = array();
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_template'] = $this->lang->line('entry_template');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
$this->load->model('admin/setting/model_setting');
if (empty($config_meta_title)) {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if (empty($config_template)) {
$data['config_template'] = $this->configs->get('config_template');
}
$data['templates'] = array();
$directories = glob(APPPATH . 'modules/catalog/views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
if (empty($config_maintenance)) {
$data['config_maintenance'] = $this->configs->get('config_maintenance');
}
$this->load->library('form_validation');
$this->form_validation->set_rules('config_meta_title', 'Meta Title');
$this->form_validation->set_rules('config_template', 'Template');
$this->form_validation->set_rules('config_maintenance', 'Maintenance');
if ($this->form_validation->run() == FALSE) {
return $this->load->view('setting/settings', $data);
} else {
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
$config_template = $this->model_setting->edit_template($this->input->post('config_template'));
$config_maintenance = $this->model_setting->edit_maintenance($this->input->post('config_maintenance'));
redirect('admin/dashboard');
}
}
}
Model Manual Change Only Now
Code: <?php
class Model_setting extends CI_Model {
public function edit_maintenance($config_maintenance) {
$data = array(
'group' => "config",
'key' => "config_maintenance",
'value' => $config_maintenance,
);
$this->db->where('setting_id', "1");
$this->db->update('setting', $data);
}
public function edit_meta_title($config_meta_title) {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $config_meta_title,
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
}
public function edit_template($config_template) {
$data = array(
'group' => "config",
'key' => "config_template",
'value' => $config_template,
);
$this->db->where('setting_id', "3");
$this->db->update('setting', $data);
}
}
|