Can't update data to database with codeigniter - php

I'm having trouble updating the data in the database with codeigniter. This is my controller :
public function update_mission_vision($id)
{
$this->securePage();
$data['mission'] = $mission = $this->about_model->get_mission_vision($id);
if ($mission == false) {
$this->session->set_flashdata('msg', 'The mission does not exist.');
redirect('about/manage-miteri-mission-vision');
} else {
if ($this->input->post('submit')) {
$this->form_validation->set_rules('title', 'title', 'required|max_length[240]');
$this->form_validation->set_rules('details', 'details', 'required|max_length[240]');
if ($this->form_validation->run()) {
$added_on = time();
$this->about_model->update_mission_vision($added_on, $id);
$this->session->set_flashdata('msg', 'mission updated successfully.');
redirect('about/manage-miteri-mission-vision');
}
}
}
$data['title'] = "$mission->title | miteripkr.com";
$data['keywords'] = "$mission->title";
$data['content'] = $this->load->view('about-us/update_mission_vision', $data, true);
$this->load->view('miteri', $data);
}
And this is my models :
function get_all_mission_vision($limit = null, $offset = null)
{
if ($limit)
{
$this->db->limit($limit. $offset);
}
return $this->db->get('mission_vision');
}
function get_mission_vision($id)
{
$this->db->where('id', $id);
$query = $this->db->get('mission_vision');
if ($query->num_rows()) {
return $query->row();
} else {
return false;
}
}
function update_mission_vision($id, $added_on)
{
$this->db->where('id', $id);
$this->db->set('title', $this->input->post('title'));
$this->db->set('details', $this->input->post('details'));
$this->db->set('added_on', $added_on);
$this->db->update('mission_vision');
}
This is my view :
<**div class="container-fluid" style="background:#fff; margin-top:10px; border-radius:5px; min-height:600px; margin-bottom:10px;">
<form action="" method="post" style="margin-top:30px;">
<?php echo $this->session->flashdata('msg');?>
<ol class="breadcrumb">
<li>admin</li>
<li>about us</li>
<li class="active">update mission & vision</li>
</ol>
<div class="form-group">
<label for="title"> Title for Post</label>
<input class="form-control" type="text" name="title" placeholder="Enter Title" value="<?php echo set_value('title', $mission->title)?>">
<?php echo form_error('title');?>
</div>
<div class="form-group">
<label for="title"> Details for Post</label>
<textarea class="ckeditor" name="details" placeholder="Enter Details" style="height:200px;"><?php echo set_value('details', $mission->details); ?></textarea>
<?php echo form_error('details');?>
</div>
<button class="btn btn-success" name="submit" type="submit" value="update"><span class="glyphicon glyphicon-ok"> update</span></button>
</form>
</div>
It even prints a success message but my data is not updated, why?

Maybe it's the call of the function update_mission_vision. You switched $id and $added_on.
And of course, like #Kamran Adil said, you have to give the post data to your model.
Update:
Here is an example how i write my update function in a model. So you can have look and make your own update function.
/**
* #name string TABLE_NAME Holds the name of the table in use by this model
*/
const TABLE_NAME = 'table';
/**
* #name string PRI_INDEX Holds the name of the tables' primary index used in this model
*/
const PRI_INDEX = 'field';
/**
* Updates selected record in the database
*
* #param Array $data Associative array field_name=>value to be updated
* #param Array $where Optional. Associative array field_name=>value, for where condition. If specified, $id is not used
* #return int Number of affected rows by the update query
*/
public function update(Array $data, $where = array()) {
if (!is_array($where)) {
$where = array(self::PRI_INDEX => $where);
}
$this->db->update(self::TABLE_NAME, $data, $where);
return $this->db->affected_rows();
}

Maybe you lost the $id in your URL.
Put a hidden input in your view with the $id, then in the controller, asign this $id = $this->input->post('id'); to get the $id. And the delete $id in your controller update_mission_vision() {

Related

Why my PHP script doesn't work in my html ? (i use codeigniter framework)

I put php script in my html (I use codeigniter), but when I show up in localhost, my php script eliminate all tag.
I want to put input text box for edit/update something and to show the previous text in my text box. But when I use php script, the element (input form and button) in tag php is missing in localhost.
<form method="post" action="<?php echo base_url('dashboard/categories_update'); ?>">
<div class="box-body">
<div class="form-group">
<label>Categories Name</label>
<?php foreach ($kategori as $k) { ?>
<input
type="hidden" name="id"
value="<?php echo base_url().$k->kategori_id; ?>"
>
<input
type="text" name="categories"
value="<?php echo base_url().$k->kategori_nama; ?>" placeholder="Type here. . . "
>
<?php } ?>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-success" value="update">
</div>
</form>
This my controller code :
public function categories()
{
$this->load->model('m_data');
$data['kategori'] = $this->m_data->get_data('kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories',$data);
$this->load->view('dashboard/v_footer');
}
public function add_categories()
{
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
public function categories_action()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() !=false) {
$categories = $this->input->post('categories');
$data = array(
'kategori_nama' => $categories,
'kategori_slug' => strtolower(url_title($categories))
);
$this->load->model('m_data');
$this->m_data->insert_data($data,'kategori');
redirect(base_url().'dashboard/categories');
} else {
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
}
public function categories_edit()
{
$id = $this->input->post('id');
$where = array(
'kategori_id' => $id
);
$this->load->model('m_data');
$data['kategori']= $this->m_data->edit_data($where,'kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_edit',$data);
$this->load->view('dashboard/v_footer');
}
public function categories_update()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() != false) {
$id = $this->input->post('id');
$kategori = $this->input->post('categories');
$where = array(
'kategori_id' => $id
);
$data = array (
'kategori_nama' => $kategori,
'kategori_slug' => strtolower(url_title($kategori))
);
$this->load->model('m_data');
$this->m_data->update_data($where,$data,'kategori');
redirect(base_url().'dashboard/categories');
}
}
there's incorrect script in my controller,
before :
public function categories_edit() {
$id = $this->input->post('id');
it should be :
public function categories_edit($id)
and now all my script is working ! thankyou guys, especially bro #RajendraSingh !

how to alert the message if the bill no is not in a database

how to alert a message if the bill no is not in the database
<form action="<?=site_url('TipUp_Loan/Bill_Delete')?>" class="form-inline" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Bill No: </label>
<input type="text" id="bill" class="form-control" name="Search1" autofocus>
</div>
</div>
<div class="modal-footer text-center">
<button type="submit" id="Delete" onclick="return confirm('Are You sure want to Delete')" class="btn btn-primary" >Delete<i class="icon-bin position-left"></i></button>
</div>
</form>
This is view code....
public function Bill_Delete(){
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$Search = $this->input->post('Search1');
$this->User_model->Bill_Delete($Search);
}
This is a controlller code...
public function Bill_Delete($Search)
{
$this->db->where('billno', $Search);
$this->db->delete('salesitem');
$this->db->where('no', $Search);
$this->db->delete('salesbill');
//echo "Successfully delted";
$this->session->set_flashdata('Add', 'You Deleted The Bill No Successfully');
redirect("Inventory/Bill_Entry","refresh");
}
This is model code...
my problem is how to find that bill no is present in the database, if it not in the database it should alert the message...
You need to use $this->db->affected_rows(); function which return true on bill is deleted else false.
Hope this will help.
//In controller function
public function Bill_Delete(){
$this->form_validation->set_rules('billno','billno','exist[salesitem.billno]');
if ($this->form_validation->run() == FALSE)
{
return $this->set_response( array(), validation_errors(), 'Bill No not exits' );
}
else
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$Search = $this->input->post('Search1');
$this->User_model->Bill_Delete($Search);
}
// In MY_Form_validation library
function exist($str, $value){
list($table, $column) = explode('.', $value, 2);
$query = $this->CI->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = $str'");
$row = $query->row();
return ($row->count > 0) ? FALSE : TRUE;
}

Prevent Duplicate Entry php with Codeigniter

I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.
The data still insert to database despite the same name.
This is the controller code
public function add(){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('add_pbk_g', $data);
}
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));
$this->load->model('mpbk_grup');
if($this->mpbk_grup->check_grup_exist('$Name')){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('layout/header', $title);
$this->load->view('add_pbk_g');
$this->load->view('layout/footer');
} else{
$this->mpbk_grup->add;
redirect('cpbk_grup/index');
}
}
}
this is the Model..
function add($data){
return $this->db->create('pbk_groups', $data);
}
function check_grup_exist($namegroup){
$this->db->where('Name', $namegroup);
$this->db->from('pbk_groups');
$query = $this->db->get();
if($query->num_rows() >0){
return $query->result();
} else {
return $query->result();
//return False;
}
}
and this is the view
<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
<div class="box-body">
<?php echo validation_errors(); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Group</label>
<div class="col-sm-10">
<input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
Kembali
</div>
<!-- /.box-footer -->
</form>
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$data = array ('Name' => $this->input->post('namegroup'));
$this->mpbk_grup->check_grup_exist($data);
}
}
Where does $Name on this line come from?
if($this->mpbk_grup->check_grup_exist('$Name')){
Also it should not have quotes around it, should be...
if($this->mpbk_grup->check_grup_exist($Name)){
Try doing this inside save function
Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);
My Model
Model_Name
$this->main_table = 'pbk_groups';
public function check_grup_exist($data)
{
$query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0)
{
$this->db->insert($this->main_table, $data);
return $this->db->insert_id();
}
else
{
return false;
}
Checking duplication data before inserting the data. Let me know if any query occurs.

Verify custom table in magento for custom module

I have made the redirect from the question i have posted here
Now i have another form in the redirected page where i need to enter name and mobile number and if it matches in my db table helloworld it will go to one page or else to another
<p>You have successfully registered</p>
<div>
<label>Login</label>
</div>
<div>
<form action="" method="post">
<label> Username </label>
<strong>:</strong>
<input class="input-text required-entry" type="text" name="fname" maxlength="20">
<label>Mobile No</label>
<strong>:</strong>
<input class="required-entry" type="number" maxlength="10" name="mobileno">
<input type="submit" name="login" value="Login">
<input type="button" name="cancel" value="Cancel">
</form>
</div>
Can any one help me how no i can verify it with existing data in db and make this work ? Shall I start it with using index controller or is there any magento way?
Update:
this is my Indexcontroler.php after below answer update
<?php
class MyCustom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
/*
* this method privides default action.
*/
public function indexAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
echo $firstname = $param['fname'];
$lastname = $param['lname'];
$address = $param['address'];
$state = $param['state'];
$city = $param['city'];
$mobile = $param['mobileno'];
$model = Mage::getModel('helloworld/helloworld');
// $model->setTitle($title);
$model->setFirstname($firstname);
$model->setLastname($lastname);
$model->setAddress($address);
$model->setState($state);
$model->setCity($city);
$model->setMobileno($mobile);
$model->save();
$this->_redirect('helloworld/index/login');
// $this->_redirectReferer();
}else {
/*
* Initialization of Mage_Core_Model_Layout model
*/
$this->loadLayout();
/*
* Building page according to layout confuration
*/
$this->renderLayout();
}
}
public function loginAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function loginnAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobileno'];
$check = Mage::getModel('helloworld/helloworld')
->AddFieldToFilter('mobileno', array('eq' => $mobile))
->AddFieldToFilter('fname', array('eq' => $username));
if(count($check)==1) {
$this->_redirectReferer();
}else {
$this->_redirect('helloworld/index/login');
}
}
}
}
you can add check like that
public function loginAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobile'];
$connectionresource = Mage::getSingleton('core/resource');
$readconnection = $connectionresource->getConnection('core_read');
$table = $connectionresource->getTableName('helloworld/helloworld');
$allrecord = $readconnection->select()->from(array('helloworld'=>$table))->where('helloworld.mobileno=?', $mobileno)
->where('helloworld.fname=?', $username);
$alldata =$readconnection->fetchAll($allrecord);
if(count($alldata)==1) {
$this->_redirect('home');
}else {
$this->_redirect('customer/account');
}
}

How do I fetch the data from database and then post it

This program will let you login first then what I need to do is to have something like edit their profile/info. I cannot fetch the data from the database, when I click the page for profile and nothing is shown.
MODEL
function login($username, $password) {
$this->db->select('username, password');
$this->db->from('tblsec');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
public function get_id($username) {
$query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");
$r = $query->result();
return $r;
}
VIEW
<?php foreach ($username as $user): ?>
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-5">
<?php echo $user->firstname; ?> <?php echo $user->lastname; ?>
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Password</label>
<div class="col-sm-5">
**********
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-5">
<?php echo $user->lastname; ?>
</div>
</div>
<br>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-5">
<?php echo $user->email; ?>
</div>
</div>
<br>
<?php endforeach; ?>
</div>
</div>
CONTROLLER
function __construct() {
parent::__construct();
$this->load->model('secretary_model', '', TRUE);
$this->load->library('form_validation');
$this->load->helper('date');
}
public function index() {
if ($this->session->userdata('logged_in')) {
$this->header();
$this->load->view('secretary/sec_login_view');
} else {
redirect('secretary/sec_login_view');
}
}
public function profile() {
if ($this->session->userdata('logged_in')) {
$username = $this->session->userdata('username');
$data['username'] = $this->secretary_model->get_ID($username);
$this->header2();
$this->load->view('secretary/secretary_profile', $data);
} else {
redirect('secretary/login', 'refresh');
}
}
First of all, Make sure you have set the logged_in session, that will ensure that the user is logged in or not. Something like
// After checking the username and password
$this->session->set_userdata('logged_in', true);
Second If username or related data not found then return the false or appropriate error in model.
/**
*#return mixed object|false
*/
function get_id($username) {
$query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");
return $query->num_rows() > 0 ? $query->result() : false;
}
Then check in your view value in your view like this:
if( is_array($username) && count($username) > 0 ) {
foreach ($username as $user):
// Set data accordingly
endforeach;
}
else {
echo "Sorry! Related Data not found!";
}
Its a good approach to fetch and debug things accordingly.

Categories