I’m trying to create an image gallery for a social networking site that I am working on for a college project but I can’t seem to get the “username” field to populate in my database. The galleryimage field populates fine, but the username field just displays “0”. I have 3 fields in my “gallery” table (id, username, and galleryimage). I’m using MVC with CodeIgniter framework. Seems to be a problem with my Active Record code. Any help would be greatly appreciated.
Here are the get/set functions in my model:
private $galleryTable = 'gallery';
function Gallery()
{
parent::__construct();
}
The putProfileGallery populates the gallery table in my database.
function putProfileGallery($username, $image)
{
$record = array('username' => $username, 'galleryimage' => $image);
$this->db->where('username', $username)->insert('gallery', $record);
}
The getProfileGallery function loops through the table and is supposed to output to the screen.
function getProfileGallery($username)
{
$this->db->select('*')->from('gallery')->where('username', $username);
$gallerySet = $this->db->get();
$gallery = array();
foreach ($gallerySet->result() as $row)
{
$gallery[] = array('username' => $row->username, 'galleryimage' => $row->galleryimage);
}
return $gallery;
}
}
You can’t combine
whereandinsert.$this->db->where('username', $username)->insert('gallery', $record);should be only$this->db->insert('gallery', $record);You should use
updateif you want to update a row.