Error 500 after submitting the form in codeigniter - php

Using these MVC, I get Error 500. Any ideas?
Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Download extends CI_Controller {
function __construct()
{
parent::__construct();
parent::__construct();
$this->load->library('form_validation');
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('upload_model');
//$this->output->enable_profiler(TRUE);
}
function index()
{
$this->form_validation->set_rules('enter_product_name',
'Enter Product Name',
'required|max_length[200]');
$this->form_validation->set_rules('test_type',
'Test Type',
'required|max_length[200]');
$this->form_validation->set_rules('test_unit',
'Test Unit',
'required|max_length[200]');
$this->form_validation->set_rules('project_code',
'Project Code',
'required|max_length[200]');
$this->form_validation->set_error_delimiters('<br /><span class="error">',
'</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$data->page = 'download_form_view';
$this->load->view('container', $data);
// $this->load->view('upload_form_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$file_name_info = $this->input->post('enter_product_name')
.'_'. $this->input->post('test_type')
.'_'. $this->input->post('test_unit')
.'_'. $this->input->post('project_code');
$filename_data = array(
'download_name' => $file_name_info
);
// run insert model to write data to db
if ($this->download_model->DownloadName($filename_data) == TRUE)
{
//redirect('download/download');
$this->download();
}
else
{
echo 'An error occurred while saving your filename to database. Please contact Admin with Issue No.[1]';
// Or whatever error handling is necessary
}
}
}
function download()
{
$this->load->helper('download');
$this->db->select('download_name');
$this->db->where("id", "0");
$this->db->limit(1);
$query = $this->db->get('downloadname');
$download_save_name = $query->row()->download_name;
$data = file_get_contents("./uploads/$download_save_name.xlsx");
force_download("$download_save_name.xlsx", $data);
}
}
?>
View
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => ''); echo
form_open('download', $attributes); ?>
<p>
<label for="enter_product_name">Enter Product Name <span class="required">*</span></label>
<?php echo form_error('enter_product_name'); ?>
<br /><input id="enter_product_name" type="text" name="enter_product_name" value="<?php echo
set_value('enter_product_name'); ?>" /> </p>
<p>
<label for="test_type">Test Type <span class="required">*</span></label>
<?php echo form_error('test_type'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'LongTerm' => 'Long Term Study',
'ShortTerm' => 'Short Term Study',
'Experimental' => 'Experimental Study',
); ?>
<br /><?php echo form_dropdown('test_type', $options, set_value('test_type'))?> </p>
<p>
<label for="test_unit">Test Unit <span class="required">*</span></label>
<?php echo form_error('test_unit'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Hyd' => 'Hyd Unit',
'Viz1' => 'Viz Unit-1',
'Viz2' => 'Viz Unit-2',
); ?>
<br /><?php echo form_dropdown('test_unit', $options, set_value('test_unit'))?> </p>
<p>
<label for="project_code">Project Code <span class="required">*</span></label>
<?php echo form_error('project_code'); ?>
<br /><input id="project_code" type="text" name="project_code" value="<?php echo set_value('project_code'); ?>" /> </p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?> </p>
<?php echo form_close(); ?>
Model
<?php
class Download_model extends CI_Model {
function __construct() { parent::__construct(); }
function DownloadName($filename_data)
{
$this->db->update('downloadname', $filename_data, "id = 0");
if ($this->db->affected_rows() == '1') { return TRUE; }
return FALSE; } } ?>

I think the problem is:
you loaded
$this->load->model('upload_model');
and in your model code you have given
class Download_model extends CI_Model {
change this to
class Upload_model extends CI_Model {

Related

form validation not working in CodeIgniter

I'm not able to run the form validation in CodeIgniter. It is not returning any errors or any submission to the database.
When i'm using
$this->form_validation->set_rules('subemail', 'Email', 'required|valid_email');
My code gets executed but not in below case. Why?
Controller name Form_sub :
class Form_sub extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('form_validation');
#loading model
$this->load->model('form_model');
$this->load->helper(array("form",'url'));
}#EOF CONSTRUCTOR
public function index(){
$this->load->view('form.php');
}//default page
public function subscribe(){
echo "hello1";
$config = array(
'field' => 'email',
'label' => 'email id',
'rules' => 'required|max_length[50]|min_length[5]|valid_email|trim|htmlspecialchars',
'errors'=>[
'required' => '* You must provide an %s',
'max_length' => '* %s should be of maximum 50 characters',
'min_length' => '* %s should be of minimum 5 characters',
'valid_email' => '* You must provide a valid %s'
]
)
$this->form_validation->set_rules($config);
echo "hello2";
if($this->form_validation->run() == false){
$this->load->view('form.php');
echo "hello3";
}
View name form.php :
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8 col-lg-8">
<?php echo form_open('form_sub/subscribe'); ?>
<input name="email" id="" type="text" value="<?php echo set_value('email');?>" class="normal" placeholder="Email Address">
<?php echo form_error('email'); ?>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4 button">
<!-- <input type="submit" id="subsubmit" name="send" class="button" value="Subscribe">-->
<input type="submit" class="button" value="Subscribe">
<?php echo form_close(); ?>
</div>
</div>
Model name form_model :
class form_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function subscribe_insert($data)
{
if($this->db->insert('subscribe',$data))
{
return true;
}
else
{
echo "something went wrong in the database";
}
}
}

Codeigniter validation errors are not displaying

My codeigniter validation errors are not displaying someone can help?
my code is
public function addProduct(){
$this->load->view('header', $this->data);
$this->load->view('product/addProduct');
$this->load->view('footer');
$this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
$this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');
if (!$this->form_validation->run() == FALSE)
{
// some stuff on validation success
}
else{
$this->load->view('product/addProduct');
}
}
and i have added
echo validation_errors(); in my view and action of the form is product/addProduct.
Try this it's work for you.
form_error() function return your form error.
$post_fields = $this->input->post();
$data['msg'] = '<ul>';
foreach ($post_fields as $k => $v) {
if (form_error($k))
$data['msg'] .= "<li>" . strip_tags(form_error($k)) . "</li>\n";
}
$data['msg'].='</ul>';
$this->load->view('product/addProduct',$data);
OR
echo validation_errors();//this function also return form error.
On view example
<?php echo validation_errors('<div class="error">', '</div>'); ?>
<!-- lower case for the controller name on form open -->
<?php echo form_open_multipart('product/addProduct');?>
<h5>productName</h5>
<input type="text" name="productName" value="<?php echo set_value('productName'); ?>" size="50" />
<h5>productPrice</h5>
<input type="text" name="productPrice" value="<?php echo set_value('productPrice'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
<?php echo form_close();?>
Controller
Make sure your file name and class name is something like this below where first letter only upper case
Guide
Filename: Product.php
<?php
class Product extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
}
public function addProduct(){
// You can get data from here also
$this->data['some'] = 'Blah';
$this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
$this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');
// remove your !
if ($this->form_validation->run() == FALSE){
// You can just display view in this area you do not have to load it multiple times
$this->load->view('header', $this->data);
$this->load->view('product/addProduct');
$this->load->view('footer');
} else {
// some stuff on validation success
}
}
}
Also check you have set your base url in config.php is required now in CI3 versions.

Not able to insert into database using CodeIgniter

I am trying to insert data into database through Codeigniter, but I am not able to proceed. I have a database mydatabase with 5 tables training_assignment_id,assignment_name, assignment_description, recordings_required, and is_active. training_assignment_id is auto-increment.
Controller-1 training.php
<?php
class Training extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper(array('form'));
$this->load->view('training_admin');
}
/*
function input()
{
$this->load->model('training');
if($this->input->post('mysubmit')){
$this->training->training_assignment_insert();
}
}*/
}
?>
Controller-2 add_training.php
<?php
class Add_training extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('training');
}
/*function index($assignment, $description, $check, $radio)
{
$assignment=$this->input->post('assignment');
$description=$this->input->post('assignment2');
$check=$this->input->post('assignment3');
$radio=$this->input->post('is_active');
//query the database
if($assignment!=NULL || $description!=NULL || $check!=NULL || $radio!=NULL)
{
$this->training->training_assignment_insert($assignment, $description, $check, $radio);
echo 'inserted successfully';
}
else {
echo "Data has not been inserted";
}
}*/
function index()
{
$this->training->training_assignment_insert(); // this should forward them to the Model
}
}
?>
Model training.php
<?php
Class Training extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function training_assignment_insert($data ){
$data = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->db->insert('training_assignment', $data);
}
}
?>
View training_admin.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>resources/training/mystyle.css">
<title>Assignment</title>
</head>
<body>
<div id="container">
<h1>Add Assignment</h1>
<div id="body">
<?php echo form_open('traning/add_training'); ?>
<div id="label">
<input type="text" name="assignment" placeholder="Assignment Name" style="margin-left: 15px;"/>
<br/>
<textarea rows="4" cols="30" name="assignment2" placeholder="Assignment Description" style="margin-left: 15px;"></textarea>
<br/>
<label>Recording Require: </label>
<select name="assignment3">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
<br/>
<label for="active">Enable </label>
<input type="radio" name="is_active" id="enable" value="1"/>
<label for="female">Disable </label>
<input type="radio" name="is_active" id="disable" value="0"/>
<br/>
<input class="class1" type="submit" value="Assign"/>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</div>
</form>
</body>
</html>
Please anyone help me I am new for the CodeIgniter.
do this
controller
<?php
class Add_training extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('training');
}
function index()
{
$insert = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->training->training_assignment_insert($insert); // this should forward them to the Model
}
}
?>
model
<?php
Class Training extends CI_Model
{
function __construct()
{
parent::__construct();
// $this->load->database();
}
function training_assignment_insert($data ){
$query = $this->db->insert('training_assignment', $data);
if($query){
return true;
}
}
}
?>
you need to pass post values to model try
controller
function index()
{
$data = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->training->training_assignment_insert($data); // this should forward them to the Model
}
and model :-
function training_assignment_insert($data ){
$this->db->insert('training_assignment', $data);
}

set query string in admin login panel in codeigniter

I have created an admin login panel. here is my model, view, controller file. If login is unsuccessful, then i want to sent a querystring. After fetching the querystring value i want to set a message
Incorrect email/password.
I think we have to modify something in catalog_model.php or anything else??
my controller(D:\wamp\www\CodeIgniter\application\controllers\admin\catalog.php)
class Catalog extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('catalog_model');
}
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->view('admin/templates/header');
$this->form_validation->set_error_delimiters('<div class="error_admin">', '</div>');
$this->load->database();
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/catalog/catalog_view');
}
else
{
$this->catalog_model->admin_login();
}
$this->load->view('admin/templates/footer');
//$html_string = $this->load->view('admin/catalog/catalog_view');
}
function logout()
{
$this->session->unset_userdata('admin_login');
$this->session->unset_userdata('admin_email');
//print_r($this->session->all_userdata());
$this->load->view('admin/catalog/catalog_view');
}
}
my model(D:\wamp\www\CodeIgniter\application\models\cataalog_model)
<?php
class Catalog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function admin_login()
{
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
);
//return $this->db->insert('school_registration', $data);
$query = $this->db->get_where('admin', $data);
$rowcount = $query->num_rows();
if($rowcount==1)
{
$this->session->set_userdata('admin_login', 'true');
$this->session->set_userdata('admin_email', $this->input->post('email'));
$this->load->view('admin/catalog/home_view');
}
else
{
$this->load->view('admin/catalog/catalog_view');
//parse_str($_SERVER['QUERY_STRING'],$_GET);
}
}
}
My view(D:\wamp\www\CodeIgniter\application\views\admin\catalog\catalog_view.php)
<?php if (!defined('BASEPATH')) exit(__('No direct script access allowed')); ?>
<div class="light_blue_back" align="center">
<div class="adminbox">
<?php echo form_open('admin/catalog'); ?>
<fieldset>
<legend><b>Admin Login</b></legend>
<dl>
<dt>
<label for="street_address1"> <span class="req">*</span>Email</label>
</dt>
<dd>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>"/><?php echo form_error('email'); ?>
</dd>
</dl>
<dl>
<dt>
<label for="street_address2">Password</label>
</dt>
<dd>
<input type="password" name="password" id="password" value="<?php echo set_value('password'); ?>"/><?php echo form_error('password'); ?>
</dd>
</dl>
<dl>
<dt>
</dt>
<dd>
<?php echo form_submit('reg_sub', 'Submit');?>
<?php echo form_reset('reg_reset', 'Reset');?>
</dd>
</dl>
</fieldset>
<?php //echo form_close('admin/catalog'); ?>
</form>
</div>
</div>
You can use set_flashdata for show your error message using this session function
the sample code is:
$this->load->library('session');
$this->session->set_flashdata('loginerror', 'Username and password mismatch');
You can show this loginerror flashdata using isset PHP function on you view page.
For more details visit this link:
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

insert and update in single form in CodeIgniter

Here I have controller news.php:
<?php
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
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 = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = "News in Detail(s)";
$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 22';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$data['updateid'] = '' ;
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();
if($update == 1){
redirect('/news', 'location');
}
}
}
public function update($id){
$this->load->helper('form');
$this->load->library('form_validation');
$data['updateid'] = $id;
$data['title'] = "Update News in Detail(s)";
$data['update'] = $this->news_model->edit_load_data($id);
$this->load->view('news/create',$data);
if ($this->input->post('submit')) {
$update = $this->news_model->update_news($id);
if($update == 1){
redirect('/news', 'location');
}
}
}
}
?>
Here my model file news_model.php:
<?php
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
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')
'text' => $this->input->post('select')
);
return $this->db->insert('news', $data);
}
public function edit_load_data($id){
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
public function update_news($id){
$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')
'text' => $this->input->post('select')
);
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}
?>
Here my form create.php file:
<?php echo validation_errors();
echo $updateid;
?>
<?php if($updateid == ''){ ?>
<?php echo form_open('news/create'); ?>
<?php } else { ?>
<?php echo form_open('news/update/'.$updateid); ?>
<?php } ?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php if($update['text']=='text1'){ echo "selected";} ?> value="text1">text1</option>
<option <?php if( $update['text']=='text2'){ echo "selected";} ?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
Here when I am going news/create in gives error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: update
Filename: news/create.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\mylab\application\views\news\create.php Line: 15
Function: _error_handler
File: C:\xampp\htdocs\mylab\application\controllers\News.php Line: 54
Function: view
File: C:\xampp\htdocs\mylab\index.php Line: 315 Function: require_once
" />
What can I do for that? I want add and edit for same file create.php
is it possible there?
I don't know that this will produce the outcome you want but it will prevent the use of undefined variables.
<?php
echo validation_errors();
$updateid = isset($updateid) ? $updateid : '';
echo $updateid;
if($updateid == '')
{
echo form_open('news/create');
}
else
{
echo form_open('news/update/'.$updateid);
}
if(!isset($update))
{
$update['title'] = NULL;
$update['slug'] = NULL;
$update['text'] = NULL;
}
?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />
<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />
<label for="text">Select</label>
<select name="select">
<option <?php
if($update['text'] == 'text1')
{
echo "selected";
}
?> value="text1">text1</option>
<option <?php
if($update['text'] == 'text2')
{
echo "selected";
}
?> value="text2">text2</option>
</select>
<br />
<input type="submit" name="submit" value="Create news item" />
</form>
You should probably modify your model so that it returns a default array if/when the inputs are not present. I am not able to identify where you are sending $update to the view either.
I think you're not returning $update['title'], $update['text'], etc.. You are returning $title, $text...
Try changing $update['title'] to $title in create.php to see what
happens

Categories