ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Tutorial News section - Cannot write to database

January 24, 2013 5:22pm

Subscribe [2]
  • #1 / Jan 24, 2013 5:22pm

    Balmaceda1330

    1 posts

    I’ve done the tutorial but when I create a new “article” it doesn’t get saved to the database. Everything seems to be fine when I create a new “article” and submit it. But when I check the database nothing is saved there. Also, I manually entered information into the database but nothing appears in the news page.
    news_model.php:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
     
    
    class News_model extends CI_Model {
    
     public function __construct() {
     
      $this->load->database("newsDB");
     }
     
     public function get_news($slug = FALSE) {
     
      if ($slug === FALSE) {
       $query = $this->db->get('news');
       return $query->result_array();
      }
       
      $query = $this->db->get_where('news', array('slug' => $slug));
      return $query->row_array();
     }
     
    
    public function set_news()
    {
     $this->load->helper('url');
     
     $slug = url_title($this->input->post('title'), 'dash', TRUE);
     
     $data = array(
      'title' => $this->input->post('title'),
      'slug' => $slug,
      'text' => $this->input->post('text')
     );
     
     return $this->db->insert('news', $data);
    }
     
    }
    
     
     
    /* End of file: ./application/models/news_model.php */

    news.php

    <?php
    class News extends CI_Controller {
    
     public function __construct()
     {
      parent::__construct();
      $this->load->model('news_model');
     }
    
     public function index()
    {
     $data['news'] = $this->news_model->get_news();
     $data['title'] = 'News archive';
     $this->load->view('templates/header', $data);
     $this->load->view('news/index', $data);
     $this->load->view('templates/footer');
    }
    
     public function view($slug)
    {
     $data['news_item'] = $this->news_model->get_news($slug);
    
     if (empty($data['news_item']))
     {
      show_404();
     }
    
     $data['title'] = $data['news_item']['title'];
    
     $this->load->view('templates/header', $data);
     $this->load->view('news/view', $data);
     $this->load->view('templates/footer');
    }  
     
     public function create()
    {
     $this->load->helper('form');
     $this->load->library('form_validation');
     
     $data['title'] = 'Create a news item';
     
     $this->form_validation->set_rules('title', 'Title', 'required');
     $this->form_validation->set_rules('text', 'text', 'required');
     
     if ($this->form_validation->run() === FALSE)
     {
      $this->load->view('templates/header', $data); 
      $this->load->view('news/create');
      $this->load->view('templates/footer');
      
     }
     else
     {
      $this->news_model->set_news('');
      $this->load->view('templates/header', $data); 
      $this->load->view('news/success');
      $this->load->view('templates/footer');
     
     }
    }
    }

    database

    $active_group = 'newsDB';
    $active_record = TRUE;
    
    $db['newsDB']['hostname'] = 'localhost';
    $db['newsDB']['username'] = 'root';
    $db['newsDB']['password'] = '';
    $db['newsDB']['database'] = 'newsDB';
    $db['newsDB']['dbdriver'] = 'mysql';
    $db['newsDB']['dbprefix'] = '';
    $db['newsDB']['pconnect'] = TRUE;
    $db['newsDB']['db_debug'] = FALSE;
    $db['newsDB']['cache_on'] = FALSE;
    $db['newsDB']['cachedir'] = '';
    $db['newsDB']['char_set'] = 'utf8';
    $db['newsDB']['dbcollat'] = 'utf8_general_ci';
    $db['newsDB']['swap_pre'] = '';
    $db['newsDB']['autoinit'] = TRUE;
    $db['newsDB']['stricton'] = FALSE;
    $db['newsDB']['port'] = 5432;

    I would appreciate it if some guided me to the right direction, or tell me what’s wrong with the code.

  • #2 / Jan 25, 2013 12:46am

    Aken

    2430 posts

    Are you sure you’re viewing the correct database when checking for updates? Your code example looks fine (from a quick skim), and if you aren’t receiving any errors, it’s kind of hard for us to narrow it down.

  • #3 / Feb 05, 2013 12:10pm

    Balmaceda1330

    1 posts

    I am viewing the right database. If I wasn’t connecting to the right database, I would get an error that that says I cannot connect to the database group. The problem I’m having is that when I write a new “article” in Create a news item (create.php) I don’t see anything written into the database. I would be okay if I could write into the database from create.php.

  • #4 / Feb 05, 2013 4:54pm

    Aken

    2430 posts

    Well, like I said, without a specific error, it’s difficult to say. If you can be more specific about what’s happening, then please do so. Otherwise you’ll just need to debug until you can find something that might help.

  • #5 / Jun 08, 2013 3:12am

    gchangetik

    5 posts

    Check your permission folder.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases