Welcome Guest, Not a member yet? Register   Sign In
[need help] insert to db..
#1

[eluser]Unknown[/eluser]
DB
Code:
CREATE TABLE `news_posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `author` varchar(30) NOT NULL,
  `title` varchar(128) DEFAULT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;

controller
Code:
<?php
class Crudberita extends Controller {

    function  __construct()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->model('crud_berita');
        $this->load->scaffolding('posts');
        $this->dx_auth->check_uri_permissions();
    }

    function index()
    {
        $data['current'] = 'crudberita';
        $data['content'] = 'backend/crudberita_view';
        $data['berita'] = $this->crud_berita->selectAll();
        $this->load->view('container', $data);
    }

     function add()
    {    
        
        if($_POST==NULL) {
            $data['current'] = 'crudberita';
            $data['content'] = 'backend/addberita_view';
            $data['username'] = $this->dx_auth->get_username();
            $this->load->view('container', $data);
        }else {
            $this->crud_berita->insert($_POST);
            redirect('crud/crudberita/index');
        }
    }

model
Code:
class crud_berita extends Model {
    function selectAll(){
        return $this->db->get('news_posts')->result();
    }  
    
    //function insert($set){
      //  $this->db->insert('news_posts', $set);
   // }
    function insert($id)
    {
        
        $data = array(
                       'id' => $this->input->post('id'),
                       'author' => ?????????, <--- I would like the author taken from the login username
                       'title' => $this->input->post('title'),
                       'content' => $this->input->post('content')
                    );

        $this->db->insert('news_posts', $data);
    }

addberita_view
Code:
&lt;form action="" method="post"&gt;
    <table>
        <tr>
            <th>id :</th>
            <td>&lt;input name="id" /&gt;&lt;/td>
        </tr>

        <tr>
             <th>author :</th>
             <td>???????????</td> <---how to get author from the login username
        </tr>
  
        <tr>
            <th>title :</th>
            <td>&lt;input name="title" /&gt;&lt;/td>
        </tr>

        <tr>
            <th>content :</th>
            <td>&lt;textarea name="content"&gt;&lt;/textarea></td>
        </tr>
        
        <tr>
            <th></th>
            <td>&lt;input type="submit" value="Tambah" /&gt;
              &lt;input name="Reset" type="reset" value="Reset" /&gt;&lt;/td>
        </tr>
    </table>
&lt;/form&gt;

for login i'm using DX_auth...I wanted to ask
- I want to get the author of the username to login, then added to the database automatically .. how to get it?? :question:
- what code I've made right? if any of what needs to be corrected?? :question:
#2

[eluser]umefarooq[/eluser]
check Dx_auth user guide there is function get_user_id() can help you to solve your problem, and its better to save user id at model, don't put in hidden filed to keep you code secure

check user guide
http://dexcell.shinsengumiteam.com/dx_au...tions.html
#3

[eluser]Unknown[/eluser]
ok i forgot .. to get the username, simply add &lt;? = $ username?&gt;, to &lt;input name='author' value='&lt;?= $username?&gt;...thank you..




Theme © iAndrew 2016 - Forum software by © MyBB