how to get next sequence value using php? - php

SELECT NEXT VALUE FOR contract_seq
//it will return next value of sequence 1, 2, 3 etc..i need to insert this sequence number to the database table.Using codeigniter i want to insert sequence number to database table
INSERT dbtest.contract (Contract_no) VALUES (NEXT VALUE FOR pestcontrol.contract_seq) // error Invalid object name 'pestcontrol.contract_seq'.

I think you haven't returned inserted id in model.Try this :
public function save_contract($data){
$insert_id = 0;
if($this->db->insert("contract", $data)){
$insert_id = $this->db->insert_id();
}
return $insert_id;
}
CONTROLLER
public function contract_submitted() {
$data = array(
'line_of_business_id' => $this->input->post('busi'),
'Contact_name' => $this->input->post('name')
);
$lastID= $this->modal_create_contract->save_contract($data);
if(!$lastID){
//Show Error
}
$data['last_id']=$lastID;
$this->load->view('contract',$data);
}

Related

Incorrect integer value: '{"id":1,"name":"Main Lab}' for column 'lab_id' at row 1

I get this error Incorrect integer value for column 'lab_id' at row 1
with dd($lab)
it returns 1
but if I fill the form and click submet it returns the error
it should be gettin from mount function?
how can I fix that?
can any one help?
public function submit()
{
$this->validate();
$this->sample->received_at = now();
$this->sample->lab_id = $this->lab;
$this->sample->save();
$this->message = "Sample {$this->samples->sample_id} Registered Successfully";
}
Shouldn't it be $this->lab->id :
$this->sample->lab_id = $this->lab;
When you set $this->lab = Lab::find($lab);, then $this->lab is an Eloquent model, not just a single id. You need to assign just the id from that model to the sample that you're creating.
$this->sample->lab_id = $this->lab->id;

Insert multiple values of input fields one column in rows in Codeigniter

I have a form which contains some input fields and checkboxes, I want to insert all the values of input in one column (rows).
Here is my form:
<form action="" method="post">
<label>Built in year</label>
<input name="feature[]">
<label>View</label>
<input name="feature[]">
here is my controller:
function create_listing(){
$this->load->view('form');
if($_POST){
$feature = array ( 'feature' => $_POST['feature'] );
foreach($feature as $fkey => $fvalue ){
$this->Mdata->f_detail($fvalue);
}
}
and here is my model:
function f_detail($fvalue){
$this->db->insert('feature',$fvalue);
return $this->db->insert_id();
}
I am getting an error :
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0, 1, 2) VALUES ('build in year', 'view', 'parking space')' at line 1
INSERT INTO `feature` (0, 1, 2) VALUES ('build in year', 'view', 'parking space')
What's wrong in my code. Anyone please tell me .
Regards
Use $this->input->post() instead of $_POST() in codeigniter both are equivalent.
Controller:
function create_listing(){
if($this->input->post()){
$feature = $this->input->post('feature');
foreach($feature as $fkey => $fvalue ){
$ids []= $this->Mdata->f_detail($fvalue);//$Ids is array of returned id
}
$this->load->view('form');//load view after database operations
}
Model:
In your model you need to specify column name like below:
function f_detail($fvalue)
{
$this->db->insert('feature',array('column_name'=>$fvalue));//specify column name
return $this->db->insert_id();
}
You can input multiple value into one column with implode function.
Controller:
function create_listing(){
if($this->input->post()){
$data = array (
'feature' => implode(",", $this->input->post('feature'))
);
$this->Mdata->f_detail($data);
}
else{
$data = array ();
$this->load->view('form', $data);
}
}
Model:
function f_detail($data){
$this->db->insert('feature',$data);
return $this->db->insert_id();
}

codeigniter auto increment foreignkey

In my application i have function to insert data to mysqldb and have more one image for one news and in database I make news id as foreign key in upload table and all inserts is work fine but the problem news FK in upload table when i insert data the first row inserted take the value i set it and the other take auto increment so the next you found code :
the inserted data in upload table
as you se the correct data is row have id 31 and the other is auto increment
the code I insert by him ;
the controller
public function insertNews() {
$this->do_one_upload();
$this->load->model('newsModel');
$this->load->model('fileModel');
$ad_ne_data = array(
'titel' => $this->input->post('ad_news_title') ,
'content' => $this->input->post('ad_news_content') ,
'news_category_id' => $this->input->post('ad_news_category') ,
'img_url' => $this->do_one_upload()[1]['full_path'],
'created_at' => date("Y-m-d")
);
$this->newsModel->addNews($ad_ne_data);
$i=0;
while($i < count($this->do_one_upload())) {
// var_dump($this->do_one_upload());
$ad_imgs_news =array(
'title' => $this->do_one_upload()[$i]['client_name'],
'file_path' => $this->do_one_upload()[$i]['full_path'],
'type' => $this->do_one_upload()[$i]['file_type'],
'size' => $this->do_one_upload()[$i]['file_size'],
'img_news_id' => $this->newsModel->getLastNewsId()
);
$i++;
$this->fileModel->addUpload($ad_imgs_news);
var_dump($ad_imgs_news);
}
}
the news model
the get Last news id inserted ;
public function getLastNewsId()
{
$last_id = $this->db->insert_id();
return $last_id;
}
the upload inset method in upload model
public function addUpload($data)
{
// $this->db->set('name', $name);
$this->db->insert('upload', $data);
}
So where the problem are in db or in code or???
Thanks, Regards
try to return
$last_id = $this->db->insert_id();
from
$this->newsModel->addNews($ad_ne_data);
Get the insert_id immediately after the query in addNews function.

Laravel 5: insert row with possibility of duplication

How to insert row with possibility of duplication in unique column and return if item inserted or not?
tried:
$id = DB::insert("INSERT IGNORE INTO question (id,text) values (10,'test')");
but $id always return with value = 1
tried also:
$id = DB::table('question')->insertGetId(['id' => 10, 'text' => 'test'])->ignore();
but ignore() has no effect!
try {
DB::table('arv_groups')->insert([
'grouphash' => $grouphash,
'id_photo' => $id
]);
} catch(\Illuminate\Database\QueryException $e){
// I DON'T CARE IT'S DUPLICATE !!! THANK YOU VERY MUCH!
}

How do I get the last id from a table after multiple insert

I am adding data to three tables, I needed to get the last ID of the first table to use in the second table, which was successful with $this->db->insert_id() function, Trying that with the second table still gives me the ID of the first table. The arrangement of my code is:
function addcrm() {
//Post data collection array from the webform form
$customerdata = array(
"salutation"=>$this->input->post('salutation'),
"mobilenumber"=>$this->input->post('mobilenumber'),
"emailaddress"=>$this->input->post('emailaddress')
);
$this->db->insert('api_customer', $customerdata);
$customer=$this->db->insert_id();
$leaddata = array(
"fieldrep"=>$this->input->post('fieldrep'),
"fk_customerID"=>$customer,
"te"=>$this->input->post('takage'),
"othercost"=>$this->input->post('othercost')
);
$this->db->insert('api_lead', $leaddata);
$leadID = $this->db->insert_id();
for ($i =0; $i<count($_POST['w_product']); $i++){
$productdata = array(
"name" => $_POST['w_product'][$i],
"type" => $_POST['w_type'][$i],
"cost" => $_POST['w_cost'][$i],
"fk_leadID"=> $leadID
);
$this->db->insert('api_prod',$productdata);
}
$url = base_url('cXXXXXXXXXXXXXX);
redirect($url);
}
You are missing something obviously in the second call. ;)
$customer = $this->db->insert_id();
$leadIdD = $this->db->insert_id;
See? :)
Try working with the following methods:
$this->db->start_cache(); // Before query
$this->db->stop_cache(); // After query
$this->db->flush_cache(); // Clear query
This way you make clear and flushed queries.
if you are using an auto increment table you may try using MYSQL_LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

Categories