Welcome Guest, Not a member yet? Register   Sign In
A Database Error Occurred You must use the "set" method to update an entry.
#1

[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>&lt;input name="firstname" type="text" id="firstname" size="30" maxlength="255" value="&lt;?=$firstname?&gt;"&gt;&lt;/td>
<td width="130px" height="160px" rowspan="6" align="center" valign="top">
<img src="&lt;?php echo base_url()." border="0"/>
&lt;?=form_hidden('bbb', $bbb)?&gt;
&lt;?=form_hidden('ccc', $ccc)?&gt;
&lt;?=form_hidden('ddd', $ddd)?&gt;
</td>
</tr>
<tr>
  <td align="right">lastname: </td><td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="&lt;?=$lastname?&gt;"&gt;&lt;/td>
  </tr>
<tr>
  <td align="right">et&agrave;: </td>
  <td>&lt;?=$age?&gt;</td>
  </tr>
<tr>
  <td align="right">nazione: </td><td>
<select name="idCountry">
&lt;? foreach ($countries->result() as $row):?&gt;
    <option value="&lt;?=$row->id?&gt;" &lt;? if ($idCountry==$row->id) echo " selected";?&gt;>
    &lt;?=$row->description?&gt;
    </option>
&lt;? endforeach?&gt;
</select>

....
#2

[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
#3

[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!!!
#4

[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.
#5

[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
#6

[eluser]pistolPete[/eluser]
Please post the full controller code.
#7

[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;
    }    
    
}
?&gt;
#8

[eluser]pistolPete[/eluser]
function _validate_form() always (in your testcase) returns TRUE.
As a result this will always be FALSE:
Code:
! $this->_validate_form()
#9

[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
#10

[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:
&lt;?php $v =& $this->validation ?&gt;

...

  <td align="right">cognome: </td><td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="&lt;?=$lastname?&gt;" class="&lt;?=$v-&gt;lastname_error?&gt;" ></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>&lt;input type="text" name="title" value="" id="title" class="field_error" /&gt;&lt;/p>


in my HTML code I've

<td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="" class="" &gt;&lt;/td>




Theme © iAndrew 2016 - Forum software by © MyBB