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

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

Related

how to get next sequence value using 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);
}

checkbox value stored in wrong place - php codeigniter

I fill my html table with these values in two rows. The row can be added dynamically.
https://i.stack.imgur.com/tc5OA.jpg
All the fields are working fine (they go to the proper place inside the database) except for the checkboxes. I checked 2 checkboxes for each row but the values all stored in the first row like this while the second row doesn't store the expected value.
https://i.stack.imgur.com/BB1QM.jpg
I named each field with array for example <input name="category[]">
this is the controller
foreach ($basic_category as $id => $key) {
$basic_data = array(
'id_number' => $id_number,
'basic_category' => $basic_category[$id],
'basic_workscope' => $basic_workscope[$id],
'basic_i' => $basic_i[$id],
'basic_e' => $basic_e[$id],
'basic_pi' => $basic_pi[$id],
'basic_pa' => $basic_pa[$id],
'basic_ata_chapter' => $basic_ata_chapter[$id]
);
$this->mod->insert_t_basic($basic_data);
}
this is the model
public function insert_t_basic($data) {
$this->db->insert('t_basic', $data);
}
Please tell me if this is not clear enough or you want more details :)
Please change all the field names in the below format
<input name="data[category][]">
<input name="data[basic_pi][]">
In the controller
$data = array();
foreach ($this->input->post('data') as $k => $v) {
foreach ($v as $data_k => $data_v) {
$data[$data_k][$k] = $data_v;
}
}
if(count($data) > 0) {
$this->mod->insert_t_basic($data);
}
In the model
$this->db->insert_batch('t_basic', $data);
Please try this Hope this helps. Thanks

Inserting session user data in php

---------------START-UPDATE-----------------
I added the session in the code and did not logout then login to refresh the variable .. my bad, thanks guy.
---------------END-UPDATE-----------------
I am using Codeigniter 3.0, I can read the session data, I have tested this by a echo. however when i try to insert into the table I get this error.
Error
Error Number: 1054
Unknown column 'LoginID' in 'field list'
INSERT INTO `Report_Comments` (`Comments`, `ReportID`, `LoginID`) VALUES (',l;', '53', NULL)
Filename: models/report/Report_model.php
Line Number: 58
Code (Model)
function create_comment()
{
$new_comment = array(
'Comments' => $this->input->post('Comments'),
'ReportID' => $this->input->post('ReportID'),
'UserID' => $this->session->userdata('LoginID')
);
$insert = $this->db->insert('Report_Comments', $new_comment);
return $insert;
}
It is always a good idea to double check that you have values assigned to variables before you use them. This revised model function provides some checking.
function create_comment()
{
$comments = $this->input->post('Comments');
$reportID = $this->input->post('ReportID');
$userID = $this->session->userdata('LoginID');
if(isset($reportID) && isset($userID))
{
$new_comment = array(
'Comments' => isset($comments) ? $comments : "",
'ReportID' => $reportID,
'UserID' => $userID
);
return $this->db->insert('Report_Comments', $new_comment);
}
return FALSE;
}
The above operates with the idea that the insert should not be attempted unless the ReportID and UserID values are set with some value. If no comments are present then an empty string will be save to the db.

CodeIgniter Getting the result of a query as a text input value

I wrote a small piece to get a value from a database table according to the input user provides. Quickly here are the events:
User inputs number to an input and submit form
That data are supposed to call the controller, and then the controller have to match and grab relevant data from the database table, and pass it to the controller again.
Then the controller must pass it to the view, where another text input (readonly) picks up the data as the value.
But what I received was an error:
Message: Undefined variable: due_amount
Filename: main/new_payment.php
Line Number: 148
Line number 148 in new_payment.php is
);
in the View.
This is my Model:
function get_by_room_number($room_data) {
$this->db->select('due_amount');
$query = $this->db->get_where('rooms', array('room_number' => $room_data), 1);
if($query->num_rows()>0) {
foreach ($query->result() as $row) {
return $row->due_amount;
}
}
This is the Controller:
function search_by_number() {
$room_data = $this->input->post('room_number');
$due_amount = $this->payments_model->get_by_room_number($room_data);
$this->index();
}
This is the View: (new_payment.php)
<?php echo form_open('payments/search_by_number'); ?>
<?php $data = array(
'name' => 'total_amount',
'id' => 'appendedPrependedInput',
'class' => 'span2',
'value' => $due_amount
); // Line Number 148
echo form_input($data);
?>
<?php echo form_close(); ?>
Try like
$data['due_amount'] = $this->payments_model->get_by_room_number($room_data);
and try to sent it to view like
$this->load->view('view_file',$data);
and at your view file echo it like
echo $due_amount;
assuming that $data is the array that you are passing to your view from your controller function.You cont pass a variable from controller to view.You need to pass it through an array data and then you can get the variable with that variable name
You should assign due_amount to $data variable and pass it to view. Like this:
$data['room_data'] = $this->input->post('room_number');
$data['due_amount'] = $this->payments_model->get_by_room_number($data['room_data']);
$this->load->view('my_view', $data);
Then in view you could do:
print_r($room_data);
print_r($due_amount);
CodeIgniter User Guide might help you understand it better.

updating a field in DB table using Codeigniter

I have two tables one is 'student' another is 'status'.The student table has a foreign key status_status_id. I want to update this status_status_field. In my view the all the status (junior, senior, fresher, sophomore) from the status are shown in a drop down menu.
I want to update the status_status_id (initially it has the value 0) field of student table by taking inputs from this drop down. And to update i also need the id of a particular student. The problem is the field status_status_id is not updating. The following error is showing-
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
UPDATE `student` SET `0` = '' WHERE `id` = Array
Filename: Z:\www\CI\system\database\DB_driver.php
Line Number: 330
my controller
function update(){
$id=$this->input->post('id');
$data=array(
'status_status_id'=>$this->input->post('status_status_id'),
);
$this->status_model->update($data, $id);
}
}
my model
function update($data,$id){
$this->db->where('id', $id);
$this->db->update('student', $data);
}
}
View for adding a status
<?php echo form_open('status_controller/update');
$r=$info[0]->id;
$data=array (
'id'=>$r,
'status_status_id'=>set_value('status_status_id')
);
?>
<p><?php echo form_input($data); ?>
<p><select name ='status_status_id'>
<?php echo form_error('status_status_id'); ?>
<br /><?php
$getType = mysql_query("SELECT status_id, status FROM status ORDER BY status_id");
while($type = mysql_fetch_object($getType)){
echo "<option value=\"{$type->status_id}\">{$type->status} </option>",set_value('status_status_id');
} ?></p><p><?php echo form_submit( 'submit', 'Update Status'); ?></p><?php echo form_close(); ?>
I see the view having few errors. I'll just rewrite it with comments, it should then work.
Notice that you should move all database queries outside the view, and of course add validation. I am not sure if ID is actually an integer.
// I suppose this gives us a single ID, not an array of IDs or something else
$r = $info[0]->id;
// put the ID in a hidden field, so POST sends an "id" variable
$hidden_fields = array('id' => $r);
// add the hidden fields directly in form_open()
echo form_open('status_controller/update', '', $hidden_fields);
// making $getType = mysql_query("SELECT status_id, status FROM status ORDER BY status_id"); in an active record way
$this->db->select('status_id, status');
$this->db->order_by('status_id', 'asc');
$query = $this->db->get('status');
// loop to make an array of the statuses to insert in form_dropdown() array('value' => 'display')
$statuses = array();
foreach ($query->result() as $row)
{
$statuses[$row->status_id] = $row->status;
}
// you would probably be able to get the current status_status_id
// I will suppose that $info[0]->status_status_id holds that data
echo form_dropdown('status_status_id', $statuses, set_value('status_status_id', $info[0]->status_status_id));
echo form_error('status_status_id');
echo form_submit( 'submit', 'Update Status');
echo form_close();
The controller:
function update(){
// again, this is suspicious from your example, check if it's not an Array
$id = $this->input->post('id');
$data = array(
'status_status_id' => $this->input->post('status_status_id'),
);
$this->status_model->update($data, $id);
}
The model:
function update($data, $id){
$this->db->where('id', $id);
$this->db->update('student', $data);
}

Categories