Welcome Guest, Not a member yet? Register   Sign In
Trying to add username along with other fields into my blog database but it keeps showing up as zero in the db
#1

[eluser]James_B[/eluser]
I have already stored the username in a session variable and when I post a blog message the message appears on the page, but the username just shows as zero in the db which means it passes to the screen as blank as well. why is this?

Model file:
<?php
class Blogmodel extends CI_Model
{
public function __construct()
{
parent::__construct();
}

public function get_last_ten_entries()
{
$query = $this->db->get('blog', 10);
return $query->result();

}

public function insert_entry()
{
$this->title = $this->input->post('title');
$this->body = $this->input->post('text');
$this->username = $this->input->post('username');
$this->date = time();

$this->db->insert('blog', $this);


}

Controller:

<?php
class Blog extends CI_Controller {

public function _construct()
{
parent::__construct();
$this->load->model('Blogmodel','Blog');
$this->load->model("profiles");

}

public function index()
{

$username = $this->session->userdata('username');

$viewData['username'] = $username;

$this->load->model('Blogmodel');


if($this->input->post('act') =='create_post')
{
$this->Blogmodel->insert_entry();

}


$viewData['blogs'] = $this->Blogmodel->get_last_ten_entries();

$this->load->view('shared/header');
$this->load->view('blog/blogtitle', $viewData);
$this->load->view('shared/nav');
$this->load->helper('form');// Load the form helper.

// Lets set the stuff that will be getting pushed forward...
$data = array();
$data['form_open']=form_open();
$data['form_title'] = form_input(array('name' => 'title'));
$data['form_text'] = form_textarea(array('name' => 'text'));
$data['form_hidden'] = form_hidden('act','create_post');
$data['form_submit'] = form_submit('submit','Make Post');

$this->load->view('blog/blogview');
$this->load->view('blog/post', $data);



$this->load->view('shared/footer');
}



}

}

View file:

<?
foreach($blogs AS $viewData)
{
$id = $viewData->id;
$title = $viewData->title;
$body = $viewData->body;
$username = $viewData->username;
?>

<b> &lt;?=$title?&gt;</b>
<p>&lt;?=$body?&gt;</p>

<p>posted by:&lt;?=$username?&gt;</p>

<hr>

&lt;?
}
?&gt;
#2

[eluser]solid9[/eluser]
Turn these,
Code:
public function insert_entry()
{
    $this->title = $this->input->post(‘title’);
      $this->body = $this->input->post(‘text’);
      $this->username = $this->input->post(‘username’);
      $this->date = time();
      
      $this->db->insert(‘blog’, $this);
      
    
  }

Into these,
Code:
public function insert_entry()
{
    $this->data['title'] = $this->input->post(‘title’);
      $this->data['body'] = $this->input->post(‘text’);
      $this->data['username'] = $this->input->post(‘username’);
      $this->data['date'] = time();
      
      $this->db->insert(‘blog’, $data);
  }

Not tested but try.
#3

[eluser]James_B[/eluser]
thanks but I ended up getting these errors:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$data

Filename: core/Model.php

Line Number: 51


A PHP Error was encountered

Severity: Notice

Message: Indirect modification of overloaded property Blogmodel::$data has no effect

Filename: models/blogmodel.php

Line Number: 18 (same for title, body, username and date)

what could the next step be here do u think?




Theme © iAndrew 2016 - Forum software by © MyBB