data getting inserted twice in database - Codeigniter - php

So I've spent hours on trying to fix this one. every time I upload a file. all the data/id3 tags are being inserted twice on my database. before it was working properly now it has this bug and I badly need some help now.
In my controller I am executing update at the same time since I want also to update the data inserted into database with the details from the id3 tags.
Controller
public function save()
{
$id = $this->session->userdata('user_id');
$status = $this->input->post('status');
$original_file = $this->input->post('original_file');
$revised_file = $this->input->post('revised_file');
$song_data = array(
'user_id' => $id,
'song_info_status' => $status,
'song_info_file_name' => $original_file,
'song_info_revised_file_name' => $revised_file,
'song_info_date_added' => 'now()',
'song_info_date_updated' => 'now()'
);
$song_info_id = $this->song_info_model->save($song_data);
$composer_data = array(
'composer_name' => ''
);
$composer_id = $this->composer_model->save($composer_data);
$song_composer_data = array(
'song_info_id' => $song_info_id,
'composer_id' => $composer_id
);
$this->song_composer_model->save($song_composer_data);
$status_data = array(
'song_info_id' => $song_info_id
);
$status_id = $this->status_model->save($status_data);
$tags_info = array();
$tags_info = $this->getTagsInfo(FCPATH ."temp" . "/" .$revised_file);
if(isset($tags_info['Author']))
{
// echo iconv("UTF-8","EUC-JP", $tags_info['Author']);
//echo $str = mb_convert_encoding($tags_info['Author'], "UTF-8", "auto");
$status_data = array(
'status_artist_name' => $tags_info['Author']
);
$this->status_model->update($status_data, $status_id);
}
if(isset($tags_info['Title']))
{
$song_data = array(
'song_info_original_title' => $tags_info['Title']
);
$this->song_info_model->update($song_data, $song_info_id);
}
rename($this->source.$revised_file, $this->destination.$revised_file);
}
In my model I am just getting the data I have from my controller. there's no loop or anything and I really can't find the culprit.
Model
public function save($data)
{
$this->db->insert('song_info', $data);
return $this->db->insert_id();
}
public function update($data, $id)
{
$this->db->where('song_info_id', $id);
$this->db->update('song_info', $data);
}

Related

i have problem with fetching data array in laravel inside controller

i have problem with fetching data after using ajax how can i fetch data and pass it to another array when i pass one field using ->value() its working but i want to pass multiple data . any help please !
code:
public function GetCoursId(Request $request)
{
/*
$idcours = $request->input('idcours');
$fcours = DB::table('cours')->where('id_cours', $idcours)->value('field_cours') ;
$idmod = DB::table('cours')->where('id_cours', $idcours)->value('id_module') ; */
$output = array(
'field_cours' => $fcours,
'id_module' => $idmod,
'idcours' => $idcours
);
echo json_encode($output); */
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->get() ;
$output = array(
'field_cours' => $PerCours->field_cours,
'id_module' => $PerCours->id_module,
'idcours' => $idcours
);
echo json_encode($output);
}
}
From your code it seems you have only one item in your collection and you want to get data from it. So use first() instead of get()
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->first() ;
$output = array(
'field_cours' => $PerCours->field_cours,
'id_module' => $PerCours->id_module,
'idcours' => $idcours
);
echo json_encode($output);
You will able to get data now like
console.log(data.field_cours);
You can use this code like this:
public function GetCoursId(Request $request)
{
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->get() ;
foreach($PerCours as $PerCour) {
$field_cours[] = $PerCour['field_cours'];
$id_module[] = $PerCour['id_module'];
}
$output = array(
'field_cours' => $field_cours,
'id_module' => $id_module,
'idcours' => $idcours
);
echo json_encode($output);
}
its working when i pass echo json_encode($PerCours);
console.log(data['0'].field_cours) ;
there is a way to display directly :
console.log(data.field_cours) ;

about laravel form calculation

I had encountered the calculation in Laravel or maybe PHP stuff, I would like to ask for solution or maybe some others approach to get the things done.
These are the codes in the Model
public function getRedeemCount($member_id){
$count = DB::table('stamps')->where('user_id', '=', $member_id)->select('stamp_counter')->first();
return $count;
}
public function getFreeOfferCount($member_id){
$free_offer_count = DB::table('stamps')->where('user_id', '=', $member_id)->select('free_offer_counter')->first();
return $free_offer_count;
}
These are the codes in the Controller
public function redeemFreeOffer(Request $request)
{
$free_offer_counter = $request->input('minus_free_offer_counter');
$member_id = $request->input('sub_id');
$total_stamp_count = $this->userModel->getRedeemCount($member_id);
$free_offer_count = $this->userModel->getFreeOfferCount($member_id);
if(!$free_offer_count){
$insert_arr = array(
'user_id' => $member_id,
'free_offer_counter' => $free_offer_counter,
'created_at'=>date("Y-m-d H:i:s")
);
$stamps = DB::table('stamps')->insert($insert_arr);
if($stamps){
$arr = array('status' => 1,'message'=>'success','value'=>$free_offer_counter);
}
}
else {
$total_stamp_count->stamp_counter;
$total_free_offer_count = floor($total_stamp_count->stamp_counter/10);
$current_offer_count = $free_offer_count->free_offer_counter - $free_offer_counter;
$update_arr = array(
'free_offer_counter' => $current_offer_count,
'updated_at' => date("Y-m-d H:i:s")
);
$stamps = DB::table('stamps')->where('user_id', $member_id)->update($update_arr);
if ($stamps) {
$arr = array('status' => 1, 'message' => 'success', 'value' => $current_offer_count);
}
}
echo json_encode($arr);
}
Since I am using jQuery AJAX, so there's too much code, but I think it should be enough information for now. If it is insufficient, I will provide more in future.
The problem is how do I inject $total_free_offer_count into $current_offer_count? I do not have any columns that stored $total_free_offer_count.
$total_stamp_count->stamp_counter;
$total_free_offer_count = floor($total_stamp_count->stamp_counter/10);
$current_offer_count = $free_offer_count->free_offer_counter - $free_offer_counter;

Not able to update mongo document with following code

Im using following code to update a mongo document. But its not working when I use $inc in order to increment a counter. It works well if no inc is used.
$arrWhere = array('epa_id' => $objData->request->memo->epa_id);
$arrSet = array(
'pa_response'=>$objData,
'response_status'=>'N',
'modified_datetime'=> getServerTimeStamp(),
array('$inc'=>array('revision_id'=>1)),
);
$arrResult = $objPriorAuth->updatePA($arrWhere, $arrSet);
public function updatePA($arrWhere,$arrSet)
{
global $medDB;
$strCollection = EPA_MASTER;
$dbCollection = $medDB->$strCollection;
$arrReturn = array();
//dump($arrSet);
try
{
$dbCollection->update(
$arrWhere,
array('$set' =>$arrSet),
array('multiple' => true)
);
dump($medDB->lastError());
}
catch(MongoCursorException $mce)
{
$arrReturn['error'] = $mce->getMessage();
logError($mce->getMessage(),DB_ERROR_LOG_FILE_PATH);
}
return $arrReturn;
}
It's wrong syntax, you can't use inc inside set. Try out the following code:
Change options:
$arrSet = array(
'pa_response'=>$objData,
'response_status'=>'N',
'modified_datetime'=> getServerTimeStamp()
);
$arrInc = array('$inc'=>array('revision_id'=>1));
Change update query:
array('$set' => $arrSet, '$inc' => $arrInc);

Looping data in controller from model codeigniter

I'm trying to prevent the user to create the same username. well my real problem is how to loop a list of data from model in controller. Maybe we know how to loop it in view by using this -> data['user'] and in view we can call $user. but how can we do that in controller layer.
here's my code
Controller
$username = strtolower($this->input->post('name'));
$fixUsername = str_replace(" ",".",$username);
$counter = 1;
$list[] = $this->addusermodel->getAllUsername();
for($i=0;$i<sizeof($list);$i++) {
if($list[$i] == $fixUsername) {
$counter = 0;
}
}
if($counter == 0) {
$data['result'] = "The username has already been taken";
$this->load->view('adduserview',$data);
} else {
$data = array(
'Nama' => $this->input->post('name'),
'Username' => $fixUsername."#praba",
'Password' => md5($this->input->post('password')),
'created' => date("Y-m-d h:i:sa"),
'createdBy' => $createdBy,
'lastModified' => date("Y-m-d h:i:sa"),
'lastModifiedBy' => $lastModifiedBy
);
$this->addusermodel->saveUser($data);
//$data['Username'] = $listName;
$data['message'] = "New user successfully added.";
$data['messageContent'] = "The username: ".$fixUsername."#praba". $counter;
$this->load->view('successpageview',$data);
//redirect('successpageview','refresh');
}
my model (is like usual)
function getAllUsername() {
$this->db->select('Username');
$this->db->from('tbluser');
$query = $this->db->get();
return $query->result_array();
}
I think a better approach would be to create another function in your model, which searches your database by ID, or by email, or by another unique field. If the function returns a row - then the user exists. If it returns nothing - then add a new user.

why mysql_query executes multiple times

i have a function and an update query in it like this :
//Article Function
function article()
{
if($_GET['action'] == "article" && !empty($_GET['id']))
{
$id = intval($_GET['id']);
$article = array();
$selectArticle = mysql_query("SELECT * FROM articles WHERE id='$id'");
$rowArticle = mysql_fetch_array($selectArticle);
$id = $rowArticle['id'];
$title = stripcslashes($rowArticle['title']);
$category = stripcslashes($rowArticle['category']);
$image = stripcslashes($rowArticle['image']);
$description = stripcslashes($rowArticle['description']);
$full_description = stripcslashes($rowArticle['full_description']);
$keywords = stripcslashes($rowArticle['keywords']);
$url = "/article/" . $rowArticle['id'] . "/" . str_replace(" ","-",stripcslashes($rowArticle['title']));
$article = array('id' => $id, 'title' => $title, 'category' => $category, 'image' => $image, 'description' => $description, 'full_description' => $full_description, 'keywords' => $keywords, 'url' => $url);
mysql_query("UPDATE articles SET visits=visits+1 WHERE id='7'");
}
return $article;
}
the function called only once but the query runs multiple times when i refresh the page.
for example in first load the visits column is 24 and after refresh the visits column is 48 !!!!!!
i cant understand why i am mixed up
1..Use header and redirect the page.
header("Location:your_page.php");
2..You can redirect to same page or different page.
Unset $_POST after inserting it to Database.
unset($_POST);

Categories