Welcome Guest, Not a member yet? Register   Sign In
Update database
#1

[eluser]anyamanggar[/eluser]
please help me, i am nubie,

i want to update my database, and my script doesnt work,
this is my controller

Code:
function edit($id = 0) {
  $this->check_logged_in();
  $this->load->model('news_model');
        if ($id == null) {
            $id = $this->input->post('id');
        }
        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('slug', 'slug', 'required');
  $this->form_validation->set_rules('intro', 'intro', 'required');
  $this->form_validation->set_rules('text', 'text', 'required');
        $this->form_validation->set_error_delimiters('', '<br/>');
        if ($this->form_validation->run() == TRUE) {

            $this->news_model->update($id);
            redirect('news_admin');
        }
        $data['page'] = $this->news_model->findById($id);

        $data['content'] = 'editnews_view';
         $this->load->view('admin/editnews_view', $data);
    }

news_model.php

Code:
function findById($id) {
        $query = $this->db->get_where('news', array('id' => $id));
        return $query->row_array();
    }

  function update($id) {
        $data = array(
          ' title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'intro' => $this->input->post('intro'),
            'text' => $this->input->post('text'),
        );
        $this->db->where('id', $id);
        $this->db->update('news', array('id' => $id));
    }

editnews_view.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Administrator | PT. Mitra Arta Semesta | Engineering and Contraction Company&lt;/title&gt;
&lt;link rel="stylesheet" href="&lt;? echo base_url(); ?&gt;script/admin.css" type="text/css"&gt;
&lt;!-- ini untuk tinyeditor --&gt;
[removed][removed]
&lt;/head&gt;

&lt;body&gt;
&lt;?php $this->load->view('admin/header_view'); ?&gt;
<h1 class="submenuadmin">Saat ini anda berada di : <strong>Main > Add News</strong></h1>

<div id="addnewsadmin">
&lt;? echo form_open('edit'); ?&gt;
&lt;?php echo form_hidden('id', $page['id']); ?&gt;
<table width="800" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td align="left" valign="top">Judul Berita</td>
    <td align="left" valign="top">
      &lt;?php echo form_input(array('name' => 'title', 'value' => set_value('title', isset($page['title']) ? $page['title'] : ''))); ?&gt;
    </td>
  </tr>

  <tr>
    <td align="left" valign="top">SEO URL</td>
    <td align="left" valign="top"> &lt;?php echo form_input(array('name' => 'slug', 'value' => set_value('slug', isset($page['slug']) ? $page['slug'] : ''))); ?&gt;<br />
      <strong><em>Form input SEO URL diisi dengan format spasi diganti dengan tanda . (titik) agar URL Friendly</em></strong></td>
  </tr>
  <tr>
    <td align="left" valign="top">Intro</td>
    <td align="left" valign="top">
       &lt;?php echo form_input(array('name' => 'intro', 'value' => set_value('intro', isset($page['intro']) ? $page['intro'] : ''))); ?&gt;
      </td>
  </tr>


  <tr>
    <td align="left" valign="top">ISi Berita</td>
    <td align="left" valign="top">&lt;?php echo form_textarea(array('name' => 'text', 'value' => set_value('text', isset($page['text']) ? $page['title'] : ''))); ?&gt;
      [removed]
   //&lt;![CDATA[

    // This call can be placed at any point after the
    // &lt;textarea&gt;, or inside a &lt;head&gt;[removed] in a
    // window.onload event handler.

    // Replace the &lt;textarea id="editor"&gt; with an CKEditor
    // instance, using default configurations.
    CKEDITOR.replace( 'text' );

   //]]>
   [removed]</td>
  </tr>
  <tr>
    <td align="left" valign="top">&nbsp;</td>
    <td  valign="top">&lt;? echo form_submit('mysubmit','Submit Berita'); ?&gt;</td>
  </tr>
</table>
&lt;? echo form_close(); ?&gt;
</div>
<br /><br />
&lt;/body&gt;
&lt;/html&gt;

please help me, i am stuck for this problem, and many thanks
#2

[eluser]njkt[/eluser]
Code:
function update($id) {
        $data = array(
          ' title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'intro' => $this->input->post('intro'),
            'text' => $this->input->post('text'),
        );
        $this->db->where('id', $id);
       [b] $this->db->update('news', array('id' => $id));[/b]
    }

This function doesn't actually update, you forgot to pass $data.
#3

[eluser]anyamanggar[/eluser]
so?, where i have to change?
#4

[eluser]nagata[/eluser]
Code:
function update($id) {
        $data = array(
          ' title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'intro' => $this->input->post('intro'),
            'text' => $this->input->post('text'),
        );
        $this->db->where('id', $id);
        $this->db->update('news', $data);
}
bases on some of the code i had for my application, this should work for you
#5

[eluser]njkt[/eluser]
[quote author="anyamanggar" date="1339742992"]so?, where i have to change?[/quote]

you may consider looking up the specific db api function here: http://ellislab.com/codeigniter/user-gui...tml#update
#6

[eluser]anyamanggar[/eluser]
[quote author="nagata" date="1339744877"]
Code:
function update($id) {
        $data = array(
          ' title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'intro' => $this->input->post('intro'),
            'text' => $this->input->post('text'),
        );
        $this->db->where('id', $id);
        $this->db->update('news', $data);
}
bases on some of the code i had for my application, this should work for you
[/quote]

erorr like this, after i used your code

Code:
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slug` = 'uuuuu', `intro` = 'uuuuu', `text` = '

\r\n uuuuu
\r\n' WHERE `id` ' at line 1

UPDATE `news` SET ` title = 'uuuuuhhh', `slug` = 'uuuuu', `intro` = 'uuuuu', `text` = '

\r\n uuuuu
\r\n' WHERE `id` = '28'

Filename: C:\AppServ\www\mitra\system\database\DB_driver.php

Line Number: 330
#7

[eluser]Unknown[/eluser]
thank you for try to helping me, this code finally work by me..

Code:
function update(){
$this->load->database();
$data = array(
'title'=>$this->input->post('title'),
'slug'=>$this->input->post('slug'),
'intro'=>$this->input->post('intro'),
'text'=>$this->input->post('text'),);
$this->db->where('id',$this->input->post('id'));
$this->db->update('news',$data);
}




Theme © iAndrew 2016 - Forum software by © MyBB