Codeigniter - duplicated results after update - php

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;
}
}

Related

Insert data from forms inside a while loop php

Good day. I just wanna ask how can I insert the data from the forms generated from a while loop. This is what I tried so far. I added a loop where they will have different id or names but when I tried on clicking the button the first form is the only one working. Thank you so much in advance.
<?php
include "../config/dbconfig.php";
$data['productCode'] = "1"; // sample data
$stmt = $conn->prepare("SELECT * FROM tbl_category");
//$stmt->bind_param("i", $data['productCode']);
$stmt->execute();
$result = $stmt->get_result();
$i = 1;
while ($stuff = $result->fetch_assoc()) {
?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?>
</div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST" name="itemform" action="">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name[<?php echo $i; ?>]" id="itemname[<?php echo $i; ?>]" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost[<?php echo $i; ?>]" id="itemcost[<?php echo $i; ?>]" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price[<?php echo $i; ?>]" id="itemprice[<?php echo $i; ?>]" placeholder="Price" required>
<input type="hidden" class="form-control" name="code[<?php echo $i; ?>]" id="forcatcode[<?php echo $i; ?>]" value="<?php echo $stuff['categorycode'] ?>">
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem" id="btnsaveitem">Save</button>
</div>
<?php $i++; ?>
<input type="hidden" name="count" value="<?php echo $i; ?>" />
</div>
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
?>
and here is my insert code
<?php
if (isset($_POST['btnsaveitem'])) {
$count = $_POST['count'];
for ($i = 1; $i < $count; $i++) {
//$code = $_POST['code'][$i]; // check empty and check if interger
$foritemname = $_POST['name'][$i]; // check empty and strip tags
//$qty = $_POST['qty'][$i]; // check empty and check if interger
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES ('".$foritemname."')");
//$stmt->bind_param("iss",$name);
$stmt->execute();
}
}
?>
Starting by rewriting your form...
You don't need $i for anything, but I'll leave the declaration in case you want it for something else.
Don't submit array type data, each form will submit its own set of fields.
It probably makes more sense to add $stuff['categorycode'] as the value of each submit to avoid needing the hidden field. I'll leave it your way for now.
Form:
foreach ($stmt->get_result() as $i => $stuff) { ?>
<div class="col-sm-6" style="margin-top:20px;">
<div class="card">
<div class="card-header"><?php echo $stuff['categoryname']; ?></div>
<div class="card-body outermydiv">
<div class="myDIV">
<form method="POST">
<div class="form-row">
<div class="col-5">
<input type="text" class="form-control" name="name" placeholder="Item name" required autocomplete="off">
</div>
<div class="col">
<input type="number" class="form-control" name="cost" placeholder="Cost" required>
</div>
<div class="col">
<input type="number" class="form-control" name="price" placeholder="Price" required>
</div>
<div class="col">
<button type="submit" class="btn btn-success" name="btnsaveitem">Save</button>
</div>
</div>
<input type="hidden" class="form-control" name="code" value="<?php echo $stuff['categorycode']; ?>">
</form>
</div>
<br>
</div>
</div>
</div>
<?php
}
Receiving script: (extend with additional fields as desired)
if (isset($_POST['btnsaveitem'])) {
$stmt = $conn->prepare("INSERT INTO tbl_items(`itemname`) VALUES (?)");
$stmt->bind_param("s",$_POST['name']);
$stmt->execute();
}
This is all untested code.

ajax is not going to controller and data is not updating

My Ajax script is not going to controller and data is not uploading.
Script is:
$("#update").click(function(event) {
/* Act on the event */
var chango = $("#update_form").serialize();
alert(chango);
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id}
},
function(data) {
console.log(data);
list_user();
});
event.preventDefault();
});
and my controller is:
public function update_profile()
{
try {
$this->load->library('form_validation');
$this->form_validation->set_rules("fname", "First Name", 'required|alpha');
$this->form_validation->set_rules("lname", "Last Name", 'required|alpha');
$this->form_validation->set_rules("mobile", "Mobile", 'required');
$this->form_validation->set_rules("dob", "Date of Birth", 'required');
$this->form_validation->set_rules("gender", "Gender", 'required');
if ($this->form_validation->run()){
$this->load->model("user");
$data=array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'mobile' => $this->input->post('mobile'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender')
);
if ($this->input->post("update")) {
$this->user->update_data($data, $this->input->post("id"));
redirect(base_url() . "home/list_user");
}
}
else
{
}
}
catch(Exception $e) {
}
$this->load->view('middlepage/update_profile.php');
}
public function update()
{
$user_id = $this->uri->segment(3);
$this->load->model("user");
$data["user_data"] = $this->user->fetch_single_data($user_id);
$data["fetch_data"] = $this->user->fetch_data1();
$this->load->view("middlepage/update_profile", $data);
}
And my model is:
public function fetch_single_data($id)
{
$this->db->where("id", $id);
$query = $this->db->get("user");
return $query;
}
public function update_data($data, $id)
{
$this->db->where("id", $id);
return $this->db->update("user", $data);
}
And my view is:
<form method="post" id="update_form" enctype="multipart/form-data" novalidate="true">
<?php if(isset($user_data))
{
foreach ($user_data->result() as $row) {
?>
<div class="form-group valid-form">
<h4>First Name:</h4>
<input type="text" class="form-control" id="fname" name="fname" value="<?php echo $row->fname ; ?>">
<span class="text-danger"><?php echo form_error("fname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Last Name:</h4>
<input type="text" class="form-control" id="lname" name="lname" value="<?php echo $row->lname ; ?>">
<span class="text-danger"><?php echo form_error("lname");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Mobile:</h4>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo $row->mobile ; ?>">
<span class="text-danger"><?php echo form_error("mobile");?></span>
</div><br>
<div class="form-group valid-form">
<h4>Date of Birth:</h4>
<input type="date" class="form-control" min="1984-01-01" max="2005-12-31" id="dob" name="dob" value="<?php echo $row->dob ; ?>">
<span class="text-danger"><?php echo form_error("dob");?></span>
</div><br>
<div class="form-group">
<h4>Gender</h4>
<div class="radio">
<label>
<input type="radio" name="gender" id="male" value="male"<?php if($row->gender == 'male') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Male</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" id="female" value="female"<?php if($row->gender == 'female') echo "checked"; ?>><span style="font-weight: bolder; font-size: 15px;">Female</span>
</label>
</div>
<span class="text-danger"><?php echo form_error("gender");?></span>
</div><br>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row->id ?>">
<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">
</div>
<?php
}
}
?>
</form>
i can't update data in database and it is not going to list table
ajax function is not working and not going to codeigniter's controller
while is try this type of similar code to submit it is work perfectly
From where you get id which you pass as data in id field
$.ajax({
type:"POST",
url:"<?php echo base_url() ?>home/update_profile",
data:{id:id} <--
},

Crud in Codeigniter not affected when Update to database

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">

Adding outputted table row to another table using codeigniter

Outputted a row from a table ,am now trying to add some of the contents to another table using a form which is inside the outputted row but its not inserted to the database. am not getting an error.
Here is my view
<?php
foreach ($h->result() as $row)
{?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
My model
function bid($data){
$query=$this->db->update('bids',$data);
return $query;
}
My controller
public function bid(){
$this->load->database();
$this->load->model('Truckeraccount_model');
$data['a']=$this->Truckeraccount_model->accepted_bid();
$data['b']=$this->Truckeraccount_model->picked_loads();
$data['h']=$this->Truckeraccount_model->loads();
$data['g']=$this->Truckeraccount_model->notification();
$data['i']=$this->Truckeraccount_model->return_loads();
$data['accepted_return_loads']=$this->Truckeraccount_model->accepted_return_loads();
$data['bid_amount']=$this->Truckeraccount_model->bid_amount(); $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('bid_amount', 'bid_amount', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('truckeraccount_view',$data);
$this->load->view('footer');
} else {
$data = array(
'truckerid' => $this->input->post('truckerid'),
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
);
$this->Truckeraccount_model->bid($data);
$data['message'] = '';
redirect('truckeraccount_ctrl/');
}
}
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</form>

value not insert into database using php codeigniter

I am trying to insert the data to the database by php CodeIgniter and display the result in the same page during submit the button. Below is my code.The problem is i got an error message as Duplicate entry '104' for key 'PRIMARY'. Please help. View Page
<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered']; ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6" rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4"> <span>SUBMIT</span>
</button>
</div>
</div>
</form>
Controller page
public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data);
if($this->form_validation->run() == FALSE)
{
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';
}
else
{
$data['ins_msg']='data not inserted';
}
}
}
Model Page
public function add_offer()
{
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}

Categories