A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
I've seen other post of forum but all problems was on update query.
I'm working on insert query
Code: function add()
{
$this->load->helper('form'); /
if ( ! $this->_validate_form() )
{
$this->template->write_view('content', 'admin/avatars', $data, TRUE);
}
else
{
$id = $this->avatars_model->add_entry();
redirect(base_url()."admin/avatars");
}
}
in the model
Code: function add_entry()
{
$this->db->insert('avatars',$_POST);
$id = $this->db->insert_id();
return $id;
}
I see the error here:
$id = $this->avatars_model->add_entry();
my view
Code: <?=form_open($formCommand);?><?=form_hidden('id', $id)?>
<table width="100%">
<tr>
<td width="100px" align="right">firstname: </td>
<td><input name="firstname" type="text" id="firstname" size="30" maxlength="255" value="<?=$firstname?>"></td>
<td width="130px" height="160px" rowspan="6" align="center" valign="top">
<img src="<?php echo base_url()." border="0"/>
<?=form_hidden('bbb', $bbb)?>
<?=form_hidden('ccc', $ccc)?>
<?=form_hidden('ddd', $ddd)?>
</td>
</tr>
<tr>
<td align="right">lastname: </td><td><input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="<?=$lastname?>"></td>
</tr>
<tr>
<td align="right">età: </td>
<td><?=$age?></td>
</tr>
<tr>
<td align="right">nazione: </td><td>
<select name="idCountry">
<? foreach ($countries->result() as $row):?>
<option value="<?=$row->id?>" <? if ($idCountry==$row->id) echo " selected";?>>
<?=$row->description?>
</option>
<? endforeach?>
</select>
....
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]pistolPete[/eluser]
Do you want to update an existing entry or add a new one?
Don't use this:
Code: $this->db->insert('avatars',$_POST);
Build the insert array by hand:
Code: $data = array();
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['id'] = $this->input->post('id');
(...)
// if you want to insert a NEW entry
$this->db->insert('avatars', $data);
// if you want to update an EXISTING entry
$this->db->where('id', $data['id']);
$this->db->update('avatars', $data);
Btw: I'd recommend using the Form validation to repopulate form: User Guide
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
Thanks for reply!!
I want add new record
in another test I've used only
$this->db->insert('avatars',$_POST);
why now I have to use:
$data = array();
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['id'] = $this->input->post('id');
sometimes I've seen direct insert from POST
Thanks!!!
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]pistolPete[/eluser]
There are (at least) two reasons:
- There are additional entries in the $_POST array (such as a submit button) which you don't save in your database.
- You might want to use XSS Filtering.
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
I've tried you solution
Model
Code: function add_entry()
{
$fields = array(
'idUser' => '1',
'idCountry' => $this->input->post('idCountry'),
'aaa' => $this->input->post('aaa'),
'bbb' => $this->input->post('bbb'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'age' => $this->input->post('age')
);
$this->db->set($fields);
$this->db->insert('avatars');
$id = $this->db->insert_id();
return $id;
}
When I select add
http://localhost:8080/app/avatars/add
Code: function add()
{
$this->template->write('title', 'asdsadas');
$this->load->helper('form');
$data['aaa']=$this->db->get('aaa');
$data['bbb']=$this->db->get('bbb');
$data['countries']=$this->db->get('countries');
//New
if ( ! $this->_validate_form() )
{
$this->template->write_view('content', 'admin/avatars', $data, TRUE);
}
else
{
$id = $this->avatars_model->add_entry();
$this->session->set_flashdata('msg', 'OK');
redirect(base_url()."admin/avatars");
}
}
I see the flash message (OK) ...without see the insert area (my avatar.php view). Where is it my error?? I have to run the view for insert new record I think... but where?
Thanks
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]pistolPete[/eluser]
Please post the full controller code.
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
Code: class Avatars extends Controller {
function Avatars()
{
parent::Controller();
$this->load->model('admin/avatars_model');
$this->load->model('admin/Login_model');
$this->load->helper("form");
$this->load->helper("url");
$this->load->library('validation');
$this->template->write('title', 'Gestione personaggi');
$this->template->write_view('sidebar', 'common/sidebar');
$this->template->write_view('header', 'common/header');
$this->template->write_view('footer', 'common/footer');
}
function index()
{
$this->autentica();
$idUtente=$this->Login_model->idUtente();
$data['avatars']=$this->avatars_model->avatars($idUtente);
$this->template->write_view('content', 'admin/avatars', $data, TRUE);
$this->template->render();
}
function add()
{
$this->template->write('title', 'NEW');
$this->load->helper('form'); //New
$data['bbb']=$this->db->get('bbb');
$data['aaa']=$this->db->get('aaa');
$data['ccc']=$this->db->get('ccc');
//New
if ( ! $this->_validate_form() )
{
$this->template->write_view('content', 'admin/avatars', $data, TRUE);
}
else
{
$id = $this->avatars_model->add_entry();
$this->session->set_flashdata('msg', 'OK');
redirect(base_url()."admin/avatars");
}
}
function _validate_form()
{
/*
$rules = array(
'category' => 'trim|required|numeric',
'title' => 'trim|required|strip_tags|xss_clean|max_length[30]',
'description' => 'trim|strip_tags|xss_clean|max_length[500]',
'url' => 'trim|required|xss_clean|prep_url|max_length[80]'
);
$this->validation->set_rules($rules);
$fields = array(
'category' => 'Category',
'title' => 'Title',
'description' => 'Description',
'url' => 'URL'
);
$this->validation->set_fields($fields);
return ($this->validation->run() == FALSE) ? FALSE : TRUE;
*/
return TRUE;
}
}
?>
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]pistolPete[/eluser]
function _validate_form() always (in your testcase) returns TRUE.
As a result this will always be FALSE:
Code: ! $this->_validate_form()
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
I tried to set the return:
TRUE -> I see the items list with the OK message
FALSE -> I see a blank page
Code: with this:
function _validate_form()
{
$rules = array(
'firstname' => 'trim|required|strip_tags|xss_clean|max_length[30]',
'lastname' => 'trim|strip_tags|xss_clean|max_length[500]'
);
$this->validation->set_rules($rules);
$fields = array(
'firstname' => 'fistname',
'lastname' => 'lastname',
'age' => 'age'
);
$this->validation->set_fields($fields);
return ($this->validation->run() == FALSE) ? FALSE : TRUE;
}
I see
A Database Error Occurred You must use the "set" method to update an entry. - El Forum - 03-15-2009
[eluser]il_dandi[/eluser]
Now it's OK!!! I missed the render!!
Now I've only 2 problems:
How Can I manage the validation... I would like apply CSS on the textbox and combobox border
Code: function add()
{
$this->load->helper('form');
$data['tipo']="aggiungi";
if ( ! $this->_validate_form() )
{
$this->template->write_view('content', 'admin/avatar', $data, TRUE);
$this->template->render();
}
else
{
$id = $this->avatars_model->add_entry();
$this->session->set_flashdata('msg', 'OK');
redirect(base_url()."admin/avatars");
}
}
function _validate_form()
{
$rules = array(
'firstname' => 'trim|required|strip_tags|xss_clean|max_length[30]',
'lastname' => 'trim|strip_tags|xss_clean|max_length[500]'
);
$this->validation->set_rules($rules);
$fields = array(
'firstname' => 'fistname',
'lastname' => 'lastname',
'age' => 'age'
);
$this->validation->set_fields($fields);
return ($this->validation->run() == FALSE) ? FALSE : TRUE;
}
in the view I have
Code: <?php $v =& $this->validation ?>
...
<td align="right">cognome: </td><td><input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="<?=$lastname?>" class="<?=$v->lastname_error?>" ></td>
</tr>
...
I would like to apply a CSS ... in linkster I've seen this solution
how can I apply the CSS?? In HTML code of linkster example I've
<p><label for="title">* Title: </label><input type="text" name="title" value="" id="title" class="field_error" /></p>
in my HTML code I've
<td><input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="" class="" ></td>
|