EllisLab text mark
Advanced Search
     
Update database
Posted: 15 June 2012 02:33 AM
Avatar
Joined: 2012-05-21
61 posts

please help me, i am nubie,

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

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

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

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

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

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

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


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

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

    // Replace the <textarea id="editor"> with an CKEditor
    // instance, using default configurations.
    
CKEDITOR.replace'text' );

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

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

 
Posted: 15 June 2012 02:41 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-05-31
6 posts
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.

 
Posted: 15 June 2012 02:49 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-05-21
61 posts

so?, where i have to change?

 
Posted: 15 June 2012 03:21 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-05-28
55 posts
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

 
Posted: 15 June 2012 03:34 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-05-31
6 posts
anyamanggar - 15 June 2012 02:49 AM

so?, where i have to change?

you may consider looking up the specific db api function here: http://ellislab.com/codeigniter/user-guide/database/active_record.html#update

 
Posted: 15 June 2012 03:44 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-05-21
61 posts
nagata - 15 June 2012 03:21 AM
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

erorr like this, after i used your code

Error Number1064

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 
`newsSET ` 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 
 
Posted: 16 June 2012 08:54 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-06-19
3 posts

thank you for try to helping me, this code finally work by me..

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);
}