codeigniter data is not getting inserted in database - php

Im a newbie in CodeIgniter and I just followed a tutorial for CRUD operations within framework but my data is not getting inserted in my database. Here is my code:
Controller: site.php
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->view("home");
}
function create()
{
$data=array('title'=>$this->input->post("title"),
'song'=>$this->input->post("song")
);
$this->site_model->add_record($data);
$this->index();
}
}
View: home.php
<html>
<head>
<title>my page</title>
<style type="text/css">
input,label
{
display:block;
}
</style>
</head>
<body>
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Song:</label>
<input type="text" name="song" id="song" />
</p>
<p>
<input type="submit" value="submit"/>
</p>
<?php echo form_close(); ?>
</body>
</html>
Model: site_model.php
<?php
class site_model extends CI_Model
{
function getAll()
{
$q=$this->db->get('name');
return $q->result();
}
function add_record($data)
{
$this->db->insert("name",$data);
return;
}
function update_record($data)
{
$this->db->where("id",14);
$this->db->update('name',$data);
}
function delete_row($data)
{
$this->db->where("id",$this->uri->segment(3));
$this->db->delete('name',$data);
}
}
My table name is name and I have 3 fields: id(auto_increment), title, and song. Help would be much appreciated.

Add this line before you calling model function.
$this->load->model('site_model');
after that call your model function
$this->site_model->add_record($data);
I think this will solve your problem
Regards
iijb

Related

column value can not be null

problem :- it is inserting data in table blank even after using form_validation
and i tried if condition for checking cat_title then it works
so basically i want to use it without using if contidtion.
i think the problem is in my controller.
this one is my controller
class Cat extends CI_Controller {
//insert in table
public function addcat()
{
$this->form_validation->set_rules('cat_title', 'Category', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/addcategory');
}
else
{
$this->load->view('admin/addcategory');
}
$data['cat_title'] = $this->input->post('cat_title');
$this->cat_model->create_cat('category',$data);
}
// print data
public function showcat()
{
$data['result'] = $this->cat_model->get_cat();
$this->load->view('admin/showcategory',$data);
}
}
?>
this is my model
<?php
class Cat_model extends CI_Model {
// get table from database
public function get_cat(){
$query = $this->db->get('category');
return $query->result();
}
public function create_cat($table,$data){
//insert data in table
$query = $this->db->insert($table,$data);
return $query;
}
}
?>
and this my form in html i create form using codeigniter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<?php $this->load->view('includes/admincss');?>
</head>
<style>
</style>
<body>
<?php $this->load->view('includes/adminheader');?>
<section>
<div class="col-lg-12 mrg-top">
<div class="row">
<?php $this->load->view('includes/header_nav');?>
<div class="col-lg-10">
<h1>Add Category</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('cat/addcat'); ?>
<div class="form-group">
<label for="pwd">Type in below Input</label>
<input type="text" class="form-control" name="cat_title">
</div>
<button type="submit" name="submit" class="btn btn-dark">Submit</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</section>
<?php $this->load->view('includes/adminfooter');?>
</body>
</html>
MySQL column is not null set it for example ,if column be nullable your code works
ALTER TABLE table_name MODIFY mycolumn varchar(255) null;

call controller method from a view file with MVC pattern in PHP

I'm writing a simple PHP website with MVC pattern (I'm not using any framework because for this website I don't want to use them).
Now I'm stuck on calling a method inside a controller from a view.
I want to create a login form, this is what I have:
My view
<?php
include_once("controller/ControllerRegLog.php");
$mycontroller = new ControllerRegLog();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header"></div>
<div id="container">
<form id='register' action='????????' method='post'>
<fieldset >
<legend>Login</legend>
<label for='username' >UserName:</label>
<input type='text' name='username' id='username' />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Here is my controller (ControllerRegLog.php)
<?php
include_once("model/Model.php");
class ControllerRegLog {
public $model;
public function __construct()
{
$this->model = new Model();
}
public function logincheck()
{
$loginresponse = $this->model->checkLogin();
}
?>
And finally my model:
<?php
class Model
{
public function checkLogin()
{
// here should go a database call to see if credentials for login are valid
}
}
?>
Look at the view. I don't know what sould be written inside the action="..." in order to pass the form values to controller for checking the credentials.
I tried with bad result (page not found) with ControllerRegLog/loginCheck and <?php echo $mycontroller."/loginCheck" ?>

Codeigniter POST Form Submit Problems

I just started learning CI and PHP and wanted to make a simple CRUD application.
I created a function to add a record, but when i submit the data in the form, Chrome downloads a file with no extension.
Controller
<?php
class Options extends CI_Controller{
function index()
{
$this->load->view('options_view');
}
function create()
{
$data = array(
'name' => $this->input->post('name'),
'price' => $this->input->post('price'));
$this->data_model->addItem($data);
$this->index();
}
}
Model
<?php
class Data_model extends CI_Model {
function getAll() {
$q = $this->db->query("SELECT * FROM items");
if($q->num_rows() >0){
foreach($q->result() as $row){
$data[]=$row;
}
}
return $data;
}
function addItem($data){
$this->db->insert('items', $data);
return;
}
}
?>
View
<html><head></head><body>
<style type="text/css">
label {display: block;}
</style>
<h2>Create</h2>
<?php echo form_open('options/create'); ?>
<p>
<label for="name">Name</label><input type="text" name="name" id="name" />
</p>
<p>
<label for="name">Price</label><input type="text" name="price" id="price" />
</p>
<p><input type="submit" value="Submit" /></p>
<?php echo form_close(); ?>
</body>
</html>
Is there something that i did wrong?
No errors pop out. The form is created, but when i add data in the textboxes and click submit, the browser dowloands a "Create" file.
Could it be that you don't need the last slash in that line?
<?php echo form_open('options/create/'); ?>
i think you need to load form helper like
$this->load->helper(array('form', 'url'));
please let me know if you face any problem.
Add $this->load->model('data_model'); to method before $this->data_model->addItem($data);

How to access data submitted from a view within the controller?

I have a very small app, it requires an id, then updates a database table based on that id. The id is an input from the view.
Basically my DB (model) has getAllProjects() and approveProject($id).
Controller:
<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class ApproveProject extends CI_Controller {
public function index () {
$this->loadView();
}
public function getData () {
$this->load->model("db_Projects");
$this->db_Projects->getAllProjects();
}
public function updateDB () {
// how can I get this variable?
$this->db_Projects->approveProject($toApprove);
}
public function loadView() {
$this->load->view("ViewProjectApproval");
}
}
?>
View:
<html language="en">
<head>
<title>Aprobare Proiect</title>
</head>
<h1> Aprobare Proiect </h1>
<body>
<div id="container">
<?php
if (isset($_POST['projectSubmit']) && ($_POST['projectSubmit'] == "Submit"))
{
$toApprove= $_POST['projectId'];
}
?>
<form action ="updateDB" method="post">
<input type="text" name="projectId">
<input type="submit" name="projectSubmit" value="Submit">
</form>
</div>
</body>
I have no idea if my view is good, or if it should be otherwise ... how can I send the ID to my controller ?
You are submitting the form using POST method so you can retrieve the variable from the POST inside your controller's method, like
$toApprove = $this->input->post('projectId');
So your method/function will look like something like this
public function updateDB () {
if($this->input->post('projectId'))
{
$toApprove = $this->input->post('projectId');
// load the model and call the method, like
$this->load->model('model_name');
$model_name->method_name($toApprove);
}
}
Update:
Your form's action doean't seem right, try this (controller_name/method_name)
<form action ="<?php echo site_url('ApproveProject/updateDB') ?>" method="post">

CodeIgniter: Sending params to a view?

My controls receives some params from the user, I would like to place them inside the view I'm calling, how should I do this without splitting the view into multiple parts?
Thank you.
If I understand correctly, you should be able to do something like this for your controller
<?php
class Blog extends Controller
{
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
And something like this for your view:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
This is from the Codeignitor User guide here
In Controller:
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view');
}
In View:
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><?= $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>
in controller
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view', $data);
}
in view
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><? echo $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>

Categories