How to get data from database table in codeigniter? - php

I need to get the data that has been stored in my database to print in my website.I want to input the user entered word and then store it in a table after that i want it to show on a page.I have already saved created a form that takes the user entered data and stores it in a table but have not been able to display the data.I want the data entered by the user be available in any file
Here is my View
<form class="form-horizontal"
action="<?php echo base_url() ?>index.php/submit/new_form_submit" method="post">
<fieldset>
<br>
<div class="form-group">
<label class="col-md-4 control-label" for="Title">Title</label>
<div class="col-md-4">
<input id="Title" name="Title" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Price">Price</label>
<div class="col-md-4">
<input id="Price" name="Price" type="number" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Describe your product</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="Link">Link to Live preview</label>
<div class="col-md-4">
<input id="Link" name="Link" type="url" placeholder="e.g http://www.example.com" class="form-control input-md" required="">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Screenshot of your theme</label>
<div class="col-md-4">
<label for="file-input">
<!-- <div class="thumbnail">
<img src="<?/*= base_url() */?>Images/placeholder.jpg"/>
</div>-->
</label>
<input id="file-input" type="file"/>
</div>
</div>
<br>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="button1id"></label>
<div class="col-md-8">
<button type="submit" class="btn btn-success">Save</button>
<a id="cancel" name="cancel" class="btn btn-danger" href="<?php echo base_url(); ?>index.php/home">
Cancel</a>
</div>
</fieldset>
Here is my Controller
<?php
session_start();
class Submit extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('security');
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Submit_Database');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
public function new_form_submit()
{
$this->load->helper('url');
$this->form_validation->set_rules('Title', 'Title', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
} else {
$data = array(
'Title' => $this->input->post('Title'),
'Price' => $this->input->post('Price'),
'textarea' => $this->input->post('textarea'),
'Link' => $this->input->post('Link')
);
$result = $this->Submit_Database->submit_insert($data);
if ($result == TRUE) {
$this->load->view('templates/header');
$this->load->view('pages/about');
$this->load->view('templates/footer');
} else {
$this->load->view('templates/header');
$this->load->view('submitf/submit');
$this->load->view('templates/footer');
}
}
redirect('');
}
}
Here is my model
<?php
Class Submit_Database extends CI_Model
{
function __construct()
{
parent::__construct(); // construct the Model class
$this->load->database();
}
// Insert registration data in database
public function submit_insert($data)
{
// Query to insert data in database
$this->db->insert('submit', $data);
}
}
Any help would be much appreciated!

The logic is this:
You need a function in model to select from the database.
A function in controller to call the model function and load the view with the data.
And a view file to actually display the data.
You can use sessions in order to store the data in a session variable and access it in any file. You have to load the session library.

Related

form validation and record insert not working in codeigniter

Form validation and record insert not working in codeigniter
model
class Mymodel extends CI_Model
{
function insert($data)
{
$this->db->insert("users", $data);
}
}
controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_controller
{
function index()
{
$this->load->view('myfolder/my_page');
}
function login()
{
$this->load->view('myfolder/login');
}
function signup()
{
$this->load->view('myfolder/signup');
}
function signupmethod()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("firstname","First Name",'requird|alpha');
$this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
$this->form_validation->set_rules("email","Email",'requird|alpha');
$this->form_validation->set_rules("password","Password",'requird|alpha');
$this->form_validation->set_rules("mobile","Mobile",'requird|alpha');
if ($this->form_validation->run())
{
$this->load->model("mymodel");
$data = array(
"firstname" => $this->input->post("firstname"),
"lastname" => $this->input->post("lastname"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"mobile" => $this->input->post("mobile"),
"curtime" => "NOW()"
);
if($this->input->post("Signup"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
}
}
public function Inserted()
{
$this->index();
}
}
?>
view(html code)
<?php include('header.php'); ?>
<form method="post" action="<?php echo base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h3>Login</h3>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>First Name</label>
<input type="text" name="firstname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("firstname"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("lastname"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Email</label>
<input type="text" name="email" value="" class="form-control">
<span class="text-danger"><?php echo form_error("email"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Password</label>
<input type="password" name="password" value="" class="form-control">
<span class="text-danger"><?php echo form_error("password"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" name="mobile" value="" class="form-control">
<span class="text-danger"><?php echo form_error("mobile"); ?></span>
</div>
<div class="col-md-6 form-group" style="margin-top: 23px;">
<input type="submit" name="submit" value="Signup" class="btn btn-info">
</div>
</div>
I have read every line carefully, code run but no errors showing and validation and record insert are not working. and no errors showing.
please help.
Change controller this block of code when you fetch form elements in controller always use the element name
if($this->input->post("submit"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
Correct spelling of required
$this->form_validation->set_rules("firstname","First Name",'required|alpha');
Your set_rules statements are incorrect. Use required instead of requird otherwise set_rules will fail.
When you are not sure why form_validation is failing, try getting the errors with:
If($this->form_validation->run() == false)
{
echo validation_error();
}

CodeIgniter PHP Fatal error: Call to undefined function form_open()

I am quite new to PHP and CodeIgniter itself. I know that this error will be encountered when you don't load the form helper.
However, I have added in and still face this error.
Please take a look at my codings.
This is my View: Create
<?php echo form_open('studCred/Create'); ?>
<?php echo validation_errors(); ?>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Admin No</label>
<input type="text" class="form-control" name="adminNo" placeholder="AdminNo">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<label>Phone number</label>
<input type="number" class="form-control" name="phone" placeholder="Phone">
</div>
</div>
</div>
<input type="submit" value="Submit" name="save" class="btn btn-skin btn-block btn-lg">
<p class="lead-footer">* We'll contact you by phone & email later</p>
</form>
This is my Controller: studCred
class studCred extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
this->load->model('studCredModeller');
}
//Register students
public function create() {
//load registration view form
$this->load->view('studCred/Create')
;
//set validation rules
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_username_exists');
$this->form_validation->set_rules('adminNo', 'AdminNo', 'required|callback_check_adminNo_exists');
$this->form_validation->set_rules('email', 'Email', 'required|callback_check_email_exists');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
if ($this->form_validation->run() === FALSE){
$this->load->view('studCred/Create');
}
else
{
this->studCredModeller->saveRecords();
this->load->view('nypportal');
}
}
}
Model: studCredModeller
if (!defined('BASEPATH'))
exit ('No direct script access allowed!');
class studCredModeller extends CI_Model
{
public function saveRecords();
{
$this->load->helper('url');
$data = array(
'username' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'));
return $this->db->insert('stud_login', $data);
}
}
Thank you. Putting it into the config/autoload.php didn't work either.
Hope this will help you :
You have series of typo error in your code, so remove them one by one and then check again
create method should be Create, add $ to here this->studCredModeller->saveRecords(); and here this->load->view('nypportal');
You are also missing url helper in controller so use url helper in controller instead of using in model
Note : controller name should start with capital letter and must match with file name, and better use url helper and form helper in autoload.php
Your controller __construct() method should be like this :
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
this->load->model('studCredModeller');
}
And Remove ; after the method saveRecords(); in your model , should be like this:
public function saveRecords()
{
$this->load->helper('url');
$data = array(
'name' =>$this->input->post('username'),
'admin_no' =>$this->input->post('adminNo'),
'email' =>$this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone')
);
return $this->db->insert('stud_login', $data);
}
for more : https://www.codeigniter.com/user_guide/general/index.html

Codeigniter - creating a function looping form with controller and method

<form method="post" action="/scheduler/run">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Controller</label>
<input type="text" class="form-control" name="form-controller" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Method</label>
<input type="text" class="form-control" name="form-method" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Seconds</label>
<input type="number" class="form-control" name="form-seconds" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Loops</label>
<input type="number" class="form-control" name="form-loops" />
</div>
</div>
</div>
</form>
I have this form data for scheduling, I wanted to run a php function which composed with controller and method, seconds is for how many seconds this works, and loops is how many times.
My attempt:
<?php
class Scheduler extends CI_Controller {
public function run()
{
$form_data = $this->input->post();
for ($i=0;$i < $form_data['form-loops'];$i++) {
$form_data['form-controller'].'/'.$form_data['form-method']; //run the function
sleep($form_data['seconds']);
}
}
}
?>
Try this:
<?php
class Scheduler extends CI_Controller {
public function run()
{
$form_data = $this->input->post();
for ($i=0;$i < $form_data['form-loops'];$i++) {
$method = $form_data['form-controller'].'/'.$form_data['form-method']; //run the function
$this->$method;
sleep($form_data['seconds']);
}
}
}
?>

Unable to update profile data into database after user login using codeigniter

Once user login into the website it will fetch the details of the login users based on profile ids.If i try to update the details of the user unable to update as well as not getting any issues.
Controller:
function index()
{
if($this->session->userdata('admin_logged_in'))
{
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['country'] = $this->signup_model->getcountry();
$data['states'] = $this->profile_model->getstates();
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
//echo '<pre>'.print_r($data, true).'</pre>';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
{
$this->load->view('welcome');
}
}
function updateprofile()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('first_name','First Name','required');
$this->form_validation->set_rules('profile_name','Profile Name','required');
$this->form_validation->set_rules('designation','Designation','required');
$this->form_validation->set_rules('address_1','Address','required');
$this->form_validation->set_rules('address_2','Address','required');
if($this->form_validation->run()== FALSE)
{
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
{
$result = $this->profile_model->update($this->input->post('profile_id'));
if(is_array($result))
{
$data['errors']=$result;
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
$this->flash->success('<h2>Successfully Updated the record.<h2>');
redirect('profile');
}
}
Model:
function getprofiledata($id)
{
$this->db->select('profile_details.*,C.country_name,S.state_name,D.city_name');
$this->db->from('profile_details');
$this->db->join('countries AS C','C.country_id=profile_details.country_id','INNER');
$this->db->join('states AS S','S.state_id=profile_details.state_id','INNER');
$this->db->join('cities AS D','D.city_id=profile_details.city_id','INNER');
$this->db->where(array('profile_details.profile_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2')
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
View:
<div class="col-md-3">
</div>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<?php
echo form_hidden('profile_id',$r->profile_id);
?>
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" nae="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="profile_name" id="profile_name" placeholder="Enter Profile name" value="<?php echo $r->profile_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="designation">Designation:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="designation" id="designation" placeholder="Enter Designation" value="<?php echo $r->designation;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="address_1">Address 1:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="address_1" name="address_1" placeholder="Enter Address Details" value="<?php echo $r->address_1;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="address_2">Address 2:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="address_2" name="address_2" placeholder="Enter Address Details" value="<?php echo $r->address_2;?>" />
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<?php endforeach;endif;?>
</div>
Once user login into the website unable to update the data into database.Once submitting the form redirecting to same page not getting any issue as well.
Dear user7047368,
Maybe problems at your model code.
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2'),
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
MUST BE (you type an extra comma in $data(array))
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2')
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
If you not sure your query is true or false, you can put this line var_dump($this->db->last_query()); into this function after $this->db->update('profile_details', $data);; then copy it and paste into Query Form in your phpmyadmin.
Hope this help.
Problem is in your view
Use this
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
instead of
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" nae="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
In your first name input field 'name' attribute has spell mistake.
Correct that 'nae' to 'name'

insert into form doesn't insert into table ? with codeigniter framework

database details:
dbtable : guests
dbcolumns : id (A_I), guestname, guestemail
when i submit a data it's only refresh the page and take no action i'm beginner please tell me where's the problem and why
i made insert form by basic php and i passed but with codeigniter maybe i'm not fully understood yet looks like my experience about 1% or 5% :D :D
Controller/Guests.php :
<?php
class Guests extends CI_Controller {
public function index($page = 'guests')
{
if ( ! file_exists(APPPATH.'/views/main/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Guests List';
$this->load->view('include/header', $data);
$this->load->model('guests_model');
$data['records'] = $this->guests_model->getAll();
$this->load->view('main/guests', $data);
$this->load->view('include/footer', $data);
}
public function addnewguest($page = 'Add New Guest')
{
if ( ! file_exists(APPPATH.'/views/main/addnewguest.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page);
$data['pagedesc'] = 'Please Fill Informations Below';
$this->load->view('include/header', $data); // loaded the header
$this->load->model('guests_model'); // loaded the model
$this->load->view('main/addnewguest', $data); // loaded add new form
if($this->input->post('createnew')) { // if click add send information to addnewguestpost() function in the guest_model file
$this->guests_model->addnewguestfunc();
}
$this->load->view('include/footer', $data); // loaded the footer
}
}
views/main/addnewguest.php
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" name="createnew">Add</button>
</div>
</div>
</form>
models/guests_model.php
<?php
class Guests_model extends CI_Model {
public function getAll() {
$query = $this->db->get('guests');
if($query->num_rows() > 0){
return $query->result();
} else {
return FALSE;
}
}
public function addnewguestfunc() {
$data = array(
'guestname' => $this->input->post('guestname'),
'guestemail' => $this->input->post('guestemail')
);
$this->db->insert('guests', $data);
}
}
Solved !!
forgot change button to input because i copied it from bootstrap basic templates for practicing ..
views/main/addnewguest.php after change button to input :
<form class="form-horizontal" action="<?php echo site_url('guests/addnewguest'); ?>" method="post">
<div class="form-group">
<label for="guestname" class="col-sm-2 control-label">Guest Name</label>
<div class="col-sm-3">
<input type="text" name="guestname" class="form-control" id="guestname" placeholder="Guest Name ...">
</div>
</div>
<div class="form-group">
<label for="guestemail" class="col-sm-2 control-label">Guest Email</label>
<div class="col-sm-3">
<input type="text" name="guestemail" class="form-control" id="guestemail" placeholder="Guest Email ..">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" name="createnew">Add</input>
</div>
</div>
</form>

Categories