$this-db-update work but Database not change - php

I am currently working on a program that uses php jquery and bootstrap, the update function, there is a an error so that database does not change but the script is running normally, Maybe somebody able to help me where is my mistake? thank you
My controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Subbidang extends CI_Controller {
public function index()
{
$this->load->model('model_subbidang');
$this->model_security->getsecurity();
$isi['content'] = 'Subbidang/tampilan_datasubbidang';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Sub Bidang';
$isi['data'] = $this->db->get('tsubbidang');
$this->load->view('tampilan_home',$isi);
}
public function tambah()
{
$this->load->model('model_subbidang');
$this->model_security->getsecurity();
$isi['content'] = 'Subbidang/form_tambahsubbidang';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Sub Bidang';
$this->load->view('tampilan_home',$isi);
}
public function simpan()
{
$this->model_security->getsecurity();
$this->load->model('model_subbidang');
$key['IDSubBidang'] = $this->input->post('kode');
$data['IDSubBidang'] = $this->input->post('kode');
$data['IDBidang'] = $this->input->post('bidang');
$data['NamaSubBidang'] = $this->input->post('subbidang');
$query = $this->db->get_where('tsubbidang',$key);
if ($query->num_rows()>0)
{
$this->db->update('tsubbidang',$key,$data);
//echo "Data Berhasil Diubah";
echo "Update";
}
else
{
$this->db->insert('tsubbidang',$data);
//xecho "Data Berhasil Disimpan";
echo 'Insert';
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
My View
<script type="text/javascript">
$(document).ready(function(){
$("#simpan").click(function(){
var string = $("#my-form").serialize();
//alert(string);
$.ajax({
type : 'POST',
url : '<?php echo site_url();?>/subbidang/simpan',
data : string,
success : function(data){
alert(data);
}
});
});
});
</script>
<form class="form-horizontal" name="my-form" id="my-form">
<div class="control-group">
<label class="control-label"bidang</label>
<div class="controls">
<select name="bidang" id="bidang">
<option value="">-Pilih-</option>
<?php
$bidang = $this->db->get('tbidang');
foreach ($bidang->result() as $row) {
?>
<option value="<?php echo $row->IDBidang;?>"><?php echo $row->Bidang;?> </option>
<?php };?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Kode Sub Bidang</label>
<div class="controls">
<input type="text" name="kode" id="kode" palceholder="kode" class="span3">
</div>
</div>
<div class="control-group">
<label class="control-label">Sub Bidang</label>
<div class="controls">
<input type="text" name="subbidang" id="subbidang" palceholder="Sub Bidang" class="span3">
</div>
</div>
<div class="controls">
<button type="button" name="simpan" id="simpan" class="btn btn-primary btn-small"> Simpan</button>
</div>
My Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_subbidang extends CI_model {
public function gettampildatasubbidang($key)
{
$this->db->where('IDBidang',$key);
$query = $this->db->get('tbidang');
if($query->num_rows()>0)
{
foreach ($query -> result() as $row) {
$hasil = $row->Bidang;
}
}
else
{
$hasi='';
}
return $hasil;
}
public function getlistsubbidang()
{
return $this->db->get('tbidang');
}
public function getdata($key)
{
$this->db->where('IDBidang',$key);
$hasil = $this->db->get('tBidang');
return $hasil;
}
public function getupdate($key,$data)
{
$this->db->where('IDBidang',$key);
$this->db->update('tBidang',$data);
}
public function getinsert($key,$data)
{
$this->db->insert('tBidang',$data);
}A
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
The echo work fine but the database doesnt change. The Insert function also work fine

You need to pass right data to update function
change
$this->db->update('tsubbidang',$key,$data);
to
$this->db->where('IDBidang', $key);
$this->db->update('tsubbidang',$data);
and Everything will be fine.
and one more thing is that you must do functionality related to database in your model not directly in controller, it is not according to the MVC structure. it destort it. and if you done everything seperetly then it is easy to understand and clear to everyone who code after you on the same.

Related

Select ajax with an app built using CodeIgniter

I have a company_model and a controller. I also have 3 corresponding tables in a database for where the data is saved and read from. The goal of this is to manage companies linked to sub holdings and the corresponding directors of the sub holdings.
When I add a new company, the Sub holding and Director fields work perfectly. My issue is when editing an already saved company, the corresponding Director field is not populated or populating. I have been trying to find the issue for sometime now, I am not receiving any console errors or Network XHR when checking with Chrome. I know this is going to be a simple fix, I can not see it or find the issue. Any advice or pointers would be greatly appreciated
The Database structure is as follows:
Table 1 is for the director information (director_id, director_name, director_subholding_id)
Table 2 is for subholding information (subholding_id, subholding_name)
Table 3 is for company information (company_id, company_name, ceo_name, company_subholding_id,
company_director_id)
Company_Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Company_model extends CI_Model{
function get_subholding(){
$query = $this->db->get('subholding');
return $query;
}
function get_director($subholding_id){
$query = $this->db->get_where('director', array('director_subholding_id' => $subholding_id));
return $query;
}
function save_company($company_name,$subholding_id,$director_id,$ceo_name){
$data = array(
'company_name' => $company_name,
'ceo_name' => $ceo_name,
'company_subholding_id' => $subholding_id,
'company_director_id' => $director_id
);
$this->db->insert('company',$data);
}
function get_company(){
$this->db->select('company_id,company_name,ceo_name,subholding_name,director_name');
$this->db->from('company');
$this->db->join('subholding','company_subholding_id = subholding_id','left');
$this->db->join('director','company_director_id = director_id','left');
$query = $this->db->get();
return $query;
}
function get_company_by_id($company_id){
$query = $this->db->get_where('company', array('company_id' => $company_id));
return $query;
}
function update_company($company_id,$company_name,$subholding_id,$director_id,$ceo_name){
$this->db->set('company_name', $company_name);
$this->db->set('ceo_name', $ceo_name);
$this->db->set('company_subholding_id', $subholding_id);
$this->db->set('company_director_id', $director_id);
$this->db->where('company_id', $company_id);
$this->db->update('company');
}
//Delete Product
function delete_company($company_id){
$this->db->delete('company', array('company_id' => $company_id));
}
Company Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Company_model','company_model');
$this->load->library('session');
}
function index(){
$data['companies'] = $this->company_model->get_company();
$this->load->view('company_list_view',$data);
}
// add new company
function add_new(){
$data['subholding'] = $this->company_model->get_subholding()->result();
$this->load->view('add_company_view', $data);
}
// get sub category by category_id
function get_director(){
$subholding_id = $this->input->post('id',TRUE);
$data = $this->company_model->get_director($subholding_id)->result();
echo json_encode($data);
}
//save company to database
function save_company(){
$company_name = $this->input->post('company_name',TRUE);
$subholding_id = $this->input->post('subholding',TRUE);
$director_id = $this->input->post('director',TRUE);
$ceo_name = $this->input->post('ceo_name',TRUE);
$this->company_model->save_company($company_name,$subholding_id,$director_id,$ceo_name);
$this->session->set_flashdata('msg','<div class="alert alert-success">Company Saved</div>');
redirect('company');
}
function get_edit(){
$company_id = $this->uri->segment(3);
$data['company_id'] = $company_id;
$data['subholding'] = $this->company_model->get_subholding()->result();
$get_data = $this->company_model->get_company_by_id($company_id);
if($get_data->num_rows() > 0){
$row = $get_data->row_array();
$data['director_id'] = $row['company_director_id'];
}
$this->load->view('edit_company_view',$data);
}
function get_data_edit(){
$company_id = $this->input->post('company_id',TRUE);
$data = $this->company_model->get_company_by_id($company_id)->result();
echo json_encode($data);
}
//update company to database
function update_company(){
$company_id = $this->input->post('company_id',TRUE);
$company_name = $this->input->post('company_name',TRUE);
$subholding_id = $this->input->post('subholding',TRUE);
$director_id = $this->input->post('director',TRUE);
$ceo_name = $this->input->post('ceo_name',TRUE);
$this->company_model-
>update_company($company_id,$company_name,$subholding_id,$director_id,$ceo_name);
$this->session->set_flashdata('msg','<div class="alert alert-success">Company Updated</div>');
redirect('company');
}
//Delete Company from Database
function delete(){
$company_id = $this->uri->segment(3);
$this->company_model->delete_company($company_id);
$this->session->set_flashdata('msg','<div class="alert alert-success">Company Deleted</div>');
redirect('company');
}
Edit Company View
<!DOCTYPE html>
<html>
<head>
<title>Edit Company</title>
<link href="<?php echo base_url().'assets/css/bootstrap.css'?>" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col col-lg-6">
<h3>Edit Company:</h3>
<form action="<?php echo site_url('company/update_company');?>" method="post">
<div class="form-group">
<label>Company</label>
<input type="text" class="form-control" name="company_name" placeholder="Company
Name" required>
</div>
<div class="form-group">
<label>Subholding</label>
<select class="form-control subholding" name="subholding" required>
<option value="">No Selected</option>
<?php foreach($subholding as $row):?>
<option value="<?php echo $row->subholding_id;?>"><?php echo $row->subholding_name;?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Director</label>
<select class="form-control director" name="director" required>
<option value="">No Selected</option>
</select>
</div>
<div class="form-group">
<label>CEO</label>
<input type="text" class="form-control" name="ceo_name" placeholder="CEO Name"
required>
</div>
<input type="hidden" name="company_id" value="<?php echo $company_id?>" required>
<button class="btn btn-success" type="submit">Update Company</button>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="<?php echo base_url().'assets/js/jquery-3.3.1.js'?>"></script>
<script type="text/javascript" src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
<script type="text/javascript">
$(document).ready(function(){
//call function get data edit
get_data_edit();
$('.subhodling').change(function(){
var id=$(this).val();
var director_id = "<?php echo $director_id;?>";
$.ajax({
url : "<?php echo site_url('company/get_director_id');?>",
method : "POST",
data : {id: id},
async : true,
dataType : 'json',
success: function(data){
$('select[name="director"]').empty();
$.each(data, function(key, value) {
if(director_id==value.director_id){
$('select[name="director"]').append('<option value="'+ value.director_id
+'" selected>'+ value.director_name +'</option>').trigger('change');
}else{
$('select[name="director"]').append('<option value="'+ value.director_id
+'">'+ value.director_name +'</option>');
}
});
}
});
return false;
});
//load data for edit
function get_data_edit(){
var company_id = $('[name="company_id"]').val();
$.ajax({
url : "<?php echo site_url('company/get_data_edit');?>",
method : "POST",
data :{company_id :company_id},
async : true,
dataType : 'json',
success : function(data){
$.each(data, function(i, item){
$('[name="company_name"]').val(data[i].company_name);
$('[name="subholding"]').val(data[i].company_subholding_id).trigger('change');
$('[name="director"]').val(data[i].company_director_id).trigger('change');
$('[name="ceo_name"]').val(data[i].ceo_name);
});
}
});
}
});
</script>
</body>
</html>
In your view, the select field for the Director is:
<select class="form-control director" name="directore" required>
Everywhere else in your view, controller and model, you're expecting a variable named director which is not really there, so when you use $this->input->post('director') you're actually getting a NULL value which explains why your updates are not working.
A simple fix would be to change your select to
<select class="form-control director" name="director" required>
and you should be ok

autocomplete gives codeigniter3 404 error

I am creating a search Bar using Jquery autocomplete in Codeigniter 3 using and ajax and MySQL
Here is Controller College.php Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class College extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('College_model');
}
public function index() {
$this->load->view('college_view');
}
function get_autocomplete() {
if (isset($_GET['term'])) {
$result = $this->college_model->search_college($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row) {
$arr_result[] = array(
'name' => $college_name,
'description' => $row->college_description,
);
echo json_encode($arr_result);
}
}
}
}
}
?>
This is College_model.php
<?php
class College_model extends CI_Model
{
function get_all_college() {
$result = $this->db->get('college');
return $result;
}
function search_college($name) {
$this->db->like('college_name', $name, 'both');
$this->db->order_by('college_name', 'ASC');
$this->db->limit(10);
return $this->db->get('college')->result();
}
}
?>
This is view page college_view.php
<div class="tab-content py-3 px-3 px-sm-0 m-auto" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<form action="http://vufind.carli.illinois.edu/vf-aru/Search/Home" method="get" role="search" target="vufind" name="searchForm">
<div class="input-group lrcInputs">
<input value="1" name="start_over" type="hidden">
<label></label>
<input class="form-control" id="college" name="college" type="text" placeholder="Search for books, ebooks, & media">
<div class="input-group-btn">
<button class="btn btn-success lrcSearchButton" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#college').autocomplete({
source: "<?php echo site_url('college/get_autocomplete'); ?>",
select: function (event, ui) {
$('[name="college"]').val(ui.item.name);
}
});
});
</script>
How to remove the error of 404 not found. if need more code or file i will help.
I m getting error in chrome console as GET http://localhost/apluscollege/college/get_autocomplete?term=as 404 (not found)
jquery.min.js
what is the isuue in the code ...I have checked from every possible angle of code.
In the controller, please write
$this->load->model('college_model');
instead of
$this->load->model('College_model');
The model name should be the same case when you load and use it

404 not found in codeigniter 3

This is controller College.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class College extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('College_model');
}
public function index() {
$this->load->view('college_view');
}
function get_autocomplete() {
if (isset($_GET['term'])) {
$result = $this->college_model->search_college($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row) {
$arr_result[] = array(
'name' => $college_name,
'description' => $row->college_description,
);
echo json_encode($arr_result);
}
}
}
}
}
?>
This is College_model.php
<?php
class College_model extends CI_Model
{
function get_all_college() {
$result = $this->db->get('college');
return $result;
}
function search_college($name) {
$this->db->like('college_name', $name, 'both');
$this->db->order_by('college_name', 'ASC');
$this->db->limit(10);
return $this->db->get('college')->result();
}
}
?>
This is view page college_view.php
<div class="tab-content py-3 px-3 px-sm-0 m-auto" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<form action="http://vufind.carli.illinois.edu/vf-aru/Search/Home" method="get" role="search" target="vufind" name="searchForm">
<div class="input-group lrcInputs">
<input value="1" name="start_over" type="hidden">
<label></label>
<input class="form-control" id="college" name="college" type="text" placeholder="Search for books, ebooks, & media">
<div class="input-group-btn">
<button class="btn btn-success lrcSearchButton" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#college').autocomplete({
source: "<?php echo site_url('college/get_autocomplete'); ?>",
select: function (event, ui) {
$('[name="college"]').val(ui.item.name);
}
});
});
</script>
How to remove the error of 404 not found. if need more code or file i will help.
i m getting error in chrome console as GET http://localhost/apluscollege/college/get_autocomplete?term=as 404 (not found) jquery.min.js
i m making a search bar using autocomplete using jQuery ana ajax

Json_encode array function Error

I have make an matrix for boxing products in to different boxed based on that assumption:
I am using PHP as an server side language and jQuery Ajax for transmitting the data.
Front end code: <input id="no_of_post" name="no_of_post" type="text" class="form-control" onChange="return test()">
<div class="col-sm-3">
<label for="lastname">Weight Box</label><br>
<input id="weight_box" name="weight_box" type="text" class="form-control">
</div>
<div class="col-sm-3">
<label for="lastname">Plate Box</label><br>
<input id="plate_box" name="plate_box" type="text" class="form-control">
</div>
<div class="col-sm-3">
<label for="lastname">Two Pipe Box</label><br>
<input id="two_pipe_box" name="two_pipe_box" type="text" class="form-control">
</div>
<div class="col-sm-3">
<label for="lastname">Four Pipe Box</label><br>
<input id="four_pipe_box" name="four_pipe_box" type="text" class="form-control" onChange="return test()">
</div>
<div class="col-sm-3">
<label for="lastname">No. Of Box</label><br>
<input id="no_of_box" name="no_of_box" type="text" class="form-control">
</div>
<script type="text/javascript">
function test(){
var no_of_post=$('#no_of_post').val();
$.ajax({
type: "POST",
url: '<?php echo base_url()?>Test/test1',
data: {no_of_post:no_of_post},
//data: $('#enqCapture').serialize(),
success: function(resp) {
// toastr.success('Success - Dispatch Detail Prepared');
$("#weight_box").val(resp[1]);
$("#plate_box").val(resp[3]);
$("#two_pipe_box").val(resp[5]);
$("#four_pipe_box").val(resp[7]);
$("#no_of_box").val(resp[9]);
console.log(resp);
},
error: function (jqXHR, exception) {
toastr.error('Error - Something wrong try again.');
}
});
return false;
}
</script>
And My Controller code is:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('Enquiry_model');
$this->load->model('Quotation_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('User');
$this->load->model('Main_model');
$this->load->model('Master_model');
$this->is_logged_in();
}
public function index(){
$data['user_data']=$this->is_logged_in();
$this->load->view('admin1/quotation/test',$data);
}
public function test1(){
$no_of_post=$this->input->post('no_of_post');
//$no_of_post."<br>";
$weight_box=0;
$plate_box=0;
$two_pipe_box=0;
$four_pipe_box=0;
if($no_of_post==1){
$weight_box=1;
$two_pipe_box=1;
$total_box=floor($weight_box+$two_pipe_box);
}else{
$weight_box=round($no_of_post/2);
$plate_box=ceil(($no_of_post/5));
$four_pipe_box=$no_of_post/4;
if (strpos($four_pipe_box,'.') !== false) {
$four_pipe_box1=floor($four_pipe_box);
$fraction=$four_pipe_box-$four_pipe_box1;
if($fraction>=.60){
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);
}else{
$four_pipe_box=floor($no_of_post/4);
$two_pipe_box=1;
$total_box=floor($weight_box+$plate_box+$four_pipe_box+$two_pipe_box);
}
}else{
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);
}
}
$arr= array($weight_box,$plate_box,$two_pipe_box,$four_pipe_box,$total_box);
/*$arr[0]=$weight_box;
$arr[1]=$plate_box;
$arr[2]=$two_pipe_box;
$arr[3]=$four_pipe_box;
$arr[4]=$total_box;*/
//$arr[0] = "Mark Reed";
//$arr[1] = "34";
//$arr[2] = "Australia";
//echo json_encode($arr);
echo json_encode(array_values($arr));
//exit();
}
}
?>
But it is return wrong result in text box.
Use:
dataType : 'json',
encode : true
After:
data: {no_of_post:no_of_post},
Because you are returning json encoded data from the controler.

JQuery Live Search in codeigniter

I want to make live search using codeigniter and jquery and mysql
but when i type something the result's not showing
here is my model code :
<?php
class artikel_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function cari_artikel($q)
{
$this->db->select('judul,contain');
$this->db->like('judul', $q);
$this->db->or_like('contain', $q);
$query = $this->db->get('artikel');
return $query->result_array();
}
}
?>
and here is my controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('artikel_model');
}
public function index()
{
if(isset($_GET['q']) && $_GET['q']){
$q = $this->input->get('q');
$this->data['data']=$this->artikel_model->cari_artikel($q);
}
else{
$this->data['data']=array_fill_keys(array('judul', 'contain'), NULL);
}
$this->data['body']='dashboard';
$this->load->view('welcome_message',$this->data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
this is my view code
<?php $this->load->helper('html');?>
<div class="row">
<div class="span12 offset3">
<form class="form-inline" name="form1" method="get" action="">
Search : <input type="text" class=span5 name="q" id="q"/> <label for="mySubmit" class="btn btn-primary"><i class="icon-search icon-white"></i></label> <input id="mySubmit" type="submit" value="Go" class="hidden" />
</form>
</div>
</div>
<?php echo br(15); ?>
<div id="result"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
var allow = true;
$(document).ready(function(){
$("#q").keypress(function(e){
if(e.which == '13'){
e.preventDefault();
loadData();
}else if($(this).val().length >= 2){
loadData();
}
});
});
function loadData(){
if(allow){
allow = false;
$("#result").html('loading...');
$.ajax({
url:'http://localhost/helpdesk?q='+escape($("#q").val()),
success: function (data){
$("#result").html(data);
allow = true;
}
});
}
}
</script>
<?php
foreach($data as $row){ ?>
<h3><?php echo $row['judul']; ?></h3>
<?php } ?>
The live search is not working.
when i type something the output just "loading..."
and the result is not showing..
If i press enter, the result will show. but that is just the usual searching method. Not live searching.
Make a new function say "live_search" that will only return the result with the ajax search and not the "index" function. This new function will return the result with the HTML markup.
public function live_search(){
if($this->input->post(null)){
//put your search code here and just "echo" it out
}
}
$.ajax({
url:'http://localhost/domain/controller/live_search',
type : 'POST',
data : { 'q' : escape($("#q").val()) },
success: function (data){
$("#result").html(data);
allow = true;
}
});
Hope it helps you.

Categories