I've been working to make some CRUD I get nothing wrong.
But when I update data I got nothing affected in database and here my code
Table : Song
Column :
id_song,
id_album,
song,
author,
composer
Code for my Model is below:
function Tampil_song_admin(){
return $this->db->get('songs');
}
function Input_song($data,$table){
$this->db->insert($table,$data);
}
function edit_song($where,$table){
return $this->db->get_where($table,$where);
}
function update_song($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
function delete_song($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
View:
<?php foreach($songs as $data){ ?>
<form action="<?php echo base_url('/index.php/Admin_song/update_song'); ?>" method="post">
<h4 class="header-title m-t-0 m-b-30"></h4>
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text" name="id_song" parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" disabled="" id="userName">
</div>
<div class="form-group">
<label for="userName">id_album</label>
<input type="text" name="id_album" parsley-trigger="change" required
value="<?php echo $data->id_album; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Songs</label>
<input type="text" name="song" parsley-trigger="change" required
value="<?php echo $data->song; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Author</label>
<input type="text" name="author" parsley-trigger="change" required
value="<?php echo $data->author; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Composer</label>
<input type="text" name="composer" parsley-trigger="change" required
value="<?php echo $data->composer; ?>" class="form-control" id="userName">
</div>
<button type="submit" class="btn btn-success waves-effect w-md waves-light m-b-5">Update</button>
<button type="button" class="btn btn-danger waves-effect w-md waves-light m-b-5">Reset</button>
</form>
<?php } ?>
And this is for Controller:
function index(){
$data['songs'] = $this->m_admin_data->Tampil_song_admin()->result();
$this->load->view('admin/v_admin_song',$data);
}
//Tambah Data
function add_song(){
$this->load->view('admin/v_admin_song_add');
}
function proses_tambah(){
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$this->m_admin_data->Input_song($data,'songs');
redirect('Admin_song/index');
}
//Update/Edit Data
function edit_song($id){
$where = array('id_song' => $id);
$data['songs'] = $this->m_admin_data->edit_song($where,'songs')->result();
$this->load->view('admin/v_admin_song_edit',$data);
}
function update_song(){
$id = $this->input->post('id_song');
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$where = array(
'id_song' => $id
);
$this->m_admin_data->update_song($where,$data,'songs');
redirect('Admin_song/index');
}
//Hapus Data
function delete_song($id){
$where = array('id_song' => $id);
$this->m_admin_data->delete_song($where,'songs');
redirect('Admin_song/index');
}
See I got nothing wrong but when I edit and try to update some data its not affected.
Hope this will help you :
You have disabled you id_song input type in your form just remove disabled attr from it
Because of this u r unable to get id_song post value in your update_song method in turn not getting in your where clause so
id_song input Should be like this :
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text"
name="id_song"
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">
</div>
Or just make it hidden if you don't want to show it like this :
<input type="hidden"
name="id_song"
parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">
Related
I have a page for adding scores of students in a school. After submitting scores of the students, the input boxes are repopulated with the results from the database.
This was done so that
Teachers who are unable to complete adding scores for students can come back and continue from where they left off.
If an edit is to be made, teachers will know where and what to be edited.
And generally for revision purposes.
However, I noticed that when an edit is made for a particular student, that subject(s) is duplicated for everyone in the class even if they have their scores already.
Please see image below to get an idea of what I mean.
MY MODEL:
public function addrn($data3) {
$this->db->insert_batch('mtscores_rn', $data3);
return $this->db->affected_rows();
}
MY CONTROLLER:
function assigngradeActionRNMT()
{
//var_dump(count($this->input->post('number')));
for($i=0; $i<count($this->input->post('number')); $i++)
{
$data3[]=array(
'section_id' => $this->input->post('section_id'),
'subject_id' => $this->input->post('subject_id'),
'class_id' => $this->input->post('class_id')[$i],
'student_id' => $this->input->post('student_id')[$i],
'session_id' => $this->input->post('session_id'),
'mt_ca1' => $this->input->post('mt_ca1')[$i],
'mt_ca2' => $this->input->post('mt_ca2')[$i],
'mt_ca3' => $this->input->post('mt_ca3')[$i],
);
}
//var_dump($data3);
$inserted = $this->mtprimary_model->addrn($data3);
if($inserted > 0)
{
$this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
//Echo back success json
redirect('admin/mtprimary/index');
}
}
MY VIEW:
<?php foreach($students as $student){ ?>
<div class="row">
<div class="col-lg-3">
<div class="form-group">
<label>Student Name</label>
<input type="hidden" name="number[]" value="">
<input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
<input type="hidden" name="session_id" value="<?php echo $student->session_id; ?>">
<input type="hidden" name="student_id[]" value="<?php echo $student->student_id; ?>">
<input type="hidden" name="class_id[]" value="<?php echo $class_id; ?>">
<input type="text" value="<?php echo $CI->GetStudentNameWithID($student->student_id); ?>" class="form-control " readonly>
</div>
</div>
<div class="col-lg-3">
<label>Class Expectation </label>
<textarea name="mt_ca1[]" class="form-control" rows="3" ><?php echo $student->mtscores? $student->mtscores->mt_ca1: 0; ?></textarea>
</div>
<div class="col-lg-3">
<label>Milestone Achieved</label>
<textarea name="mt_ca2[]" class="form-control" rows="3" ><?php echo $student->mtscores? $student->mtscores->mt_ca2: 0; ?></textarea>
</div>
<div class="col-lg-2">
<div class="form-group">
<label>Remark</label>
<input type="text" name="mt_ca3[]" class="form-control" value="<?php echo $student->mtscores? $student->mtscores->mt_ca3: 0; ?>" >
</div>
</div>
</div>
Use this Updated Code in Your Model
public function addrn($data3)
{
$statusIns=$this->checkAlreadyExistOrNor();
if($statusIns==0){
$this->db->insert_batch('mtscores_rn', $data3);
}else{
$this->db->where($data3);
$this->db->update('mtscores_rn', $data3);
}
return $this->db->affected_rows();
}
public function checkAlreadyExistOrNor($data3){
$this->db->select(*);
$this->db->where($data3);
$result=$this->db->get();
$listData=$result->result_array();
if(!empty($listData)){
return 1;
}else{
return 0;
}
}
In my template part I am using a function which looks like this:
function job_listings($id = null) {
$output = '';
$args = array(
'post_type' => 'job-listings',
);
$loop = new WP_Query($args);
while($loop->have_posts()) {
$loop->the_post();
if($id == get_the_id()) {
$output .= '<option selected value="'.get_the_ID().'">'.get_the_title().'</option>';
} else {
$output .= '<option value="'.get_the_ID().'">'.get_the_title().'</option>';
}
}
wp_reset_postdata();
return $output;
}
...and I have struggled to realize what was the problem but I see now that every time I try to use the_ID() or get_the_ID() after the function above the ID prints ID from the particular function which I don't get because I thought the wp_reset_postdata() is closing the query.
For example:
<form id="job-apply-<?php the_ID(); ?>" action="" method="post" autocomplete="off" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
<div class="form-group">
<label for="full-name">Full Name:</label>
<input type="text" class="form-control form-control-lg" id="full-name">
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control form-control-lg" id="email">
</div>
<div class="form-group">
<label for="sel1">Select Position:</label>
<select class="form-control form-control-lg" id="job-position">
<?php job_listings(get_the_ID()); ?>
</select>
</div>
<div class="form-group">
<label class="control-label">Upload Resume</label>
<input type="file" class="filestyle" data-buttonText="Select a File" id="resume-<?php the_ID(); ?>">
</div>
<div class="form-group">
<label for="comment">Additional Comments:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<button type="submit" class="btn btn-default btn-lg">Apply</button>
</form>
in the id of the form id="job-apply-<?php the_ID(); ?>" prints the current post's ID like it should, but after the use of the function job_listings() in the id of the input id="resume-<?php the_ID(); ?>" prints the last ID in the above mentioned function's query.
Any help would be appreciated.
Change ID in your if statement. Change the get_the_id() to $loop->ID use the ID inside the $loop.
if($id == $loop->ID) {
$output .= '<option selected value="'.$loop->ID.'">'.get_the_title($loop->ID).'</option>';
} else {
$output .= '<option value="'.$loop->ID.'">'.get_the_title($loop->ID).'</option>';
}
myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.
I'm trying to do update profile from a session. I got confused because I can print username because of session. but password, Full Name, and level doesn't match with database.
It should be
Username : Ratih | Full Name : Ratih Cha | Level : Sales
But the result
Username : Ratih | Full Name : Administrator | Level : Admin
Here is my Controller:
public function profile(){
$uprofile = $this->salesModel->openProfile();
$data = array(
"username" => $uprofile[0]['username'],
"fullname" => $uprofile[0]['fullname'],
"level" => $uprofile[0]['level'],
"pic" => $uprofile[0]['pic']
);
$this->load->view('v_navbar');
$this->load->view('v_leftside');
$this->load->view('v_profile', $data);
}
Model
public function openProfile(){
$username = $this->session->userdata('username');
$data = $this->db->get('tuser');
$this->db->where('username', $username);
return $data->result_array();
}
View
<form role="form" action="<?php echo base_url()."index.php/SalesAdmin/update_profile/"?>" method="post">
<div class="form-group">
<label> Username : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $_SESSION['username']; ?>" readonly>
</div>
<div class="form-group">
<label> Full Name : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $fullname; ?>">
</div>
<div class="form-group">
<label> Level : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $level; ?>" readonly>
</div>
<div class="form-group">
<img data-name="<?php echo $_SESSION['username']; ?>" id="profile" class="img-circle" alt="Avatar" width="40px" height="32px"/><span></span></i>[enter image description here][1]
<?php echo form_open_multipart('upload/do_upload');?>
<?php echo form_upload('pic'); ?>
</div>
<div>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
I am not sure where i went wrong here but please bear with me, this is the url that comes up after click on the save button, http://localhost/gabbyville/tenant/contact/update/contact/save all want to happen, is to change the existing data on a row from a database. can anyone please help me?
here is the controller
public function update($id)
{
$contact['contact'] = $this->Contacts_model->get_all();
$contacts['contacts'] = $this->Contacts_model->get_by_id($id);
$data['content'] = $this->load->view("tenant/contacts",$contacts,TRUE);
$data['content'] = $this->load->view("tenant/contacts",$contact,TRUE);
$data['curpage'] = $this->curpage;
$this->load->view('template1',$data);
}
public function save($id)
{
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'postal_address' => $this->input->post('postal_address'),
'company_name' => $this->input->post('company_name'),
'company_url' => $this->input->post('company_url'),
'industry' => $this->input->post('industry'),
'phone_number' => $this->input->post('phone_number'),
);
$this->Contacts_model->update($this->uri->segment(), $data);
redirect($this->view_folder.$this->curpage, 'refresh');
}
here is the model
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
here is the form from my view
<form action="contact/save" method="post">
<div class="row ">
<input type="text" class="form-control mBottom2 mTop2" name="first_name" placeholder="First Name" value="<?php echo $contacts->first_name?>">
<input type="text" class="form-control mBottom2" name="last_name" placeholder="Last Name" value="<?php echo $contacts->last_name?>">
<input type="text" class="form-control mBottom2" name="postal_address" placeholder="Postal Address" value="<?php echo $contacts->postal_address?>">
<input type="text" class="form-control mBottom2" name="company_name" placeholder="Company Name" value="<?php echo $contacts->company_name?>">
<input type="text" class="form-control mBottom2" name="company_url" placeholder="Company URL" value="<?php echo $contacts->company_url?>">
<input type="text" class="form-control mBottom2" name="industry" placeholder="Industry" value="<?php echo $contacts->industry?>">
<ul class="row contact">
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" name="phone_number" placeholder="Phone Number" value="<?php echo $contacts->phone_number?>">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
</li>
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Enter New Phone Number">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
<a><i class="fa fa-trash-o"></i></a>
</li>
<li class="">
<button type="submit" class="btn btn-w-m btn-primary">Add New</button>
</li>
</ul>
</form>
<ul class="row contact">
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Email">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
</li>
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Enter New Email">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
<a><i class="fa fa-trash-o"></i></a>
</li>
<li class="mBottom2">
<button type="button" class="btn btn-w-m btn-primary">Add New</button>
</li>
</ul>
</div>
Use only one model to update or insert the data into the table:
Your Model should be like this:
public function save($data = array())
{
if(isset($data['id']) && ((int)$data['id'] > 0))
{
$this->db->where('id', (int)$data['id']);
$this->db->update('table_name', $data);
$id = $data['id'];
}
else
{
$this->db->insert('table_name', $data);
$id = $this->db->insert_id();
}
return $id;
}
And send values from controller to model like this:
$data = array(
'id' => ($this->input->post('id')) ? $this->input->post('id') : 0,
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'postal_address' => $this->input->post('postal_address'),
'company_name' => $this->input->post('company_name'),
'company_url' => $this->input->post('company_url'),
'industry' => $this->input->post('industry'),
'phone_number' => $this->input->post('phone_number'),
);