TPLMedia24 TPLMedia24 - 8 days ago 4
Javascript Question

A Database Error Occurred How to submit same name Input field using jQuery in codeigniter

I am new to stackoverflow. So, forgive me for anything wrong. I am not so expert in programming area.

I am trying to build an small application using Codeigniter. I do not not how to pass dynamic same input field value using jQuery to Codeigniter controller to model. I am using this jQuery plugin to make dynamic input field.

For test purpose, I have pasted same html input code more than 3times.

Error:


A Database Error Occurred

Error Number: 1064

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 ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('',' at line 1

INSERT INTO
products
(0, 1, 2) VALUES ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('','','')

Filename: C:/xampp/htdocs/store_management/system/database/DB_driver.php

Line Number: 691


My code:

function add_product($params)
{
$this->db->insert_batch('products',$params);
return $this->db->insert_id();
}



function add()
{
if(isset($_POST) && count($_POST) > 0)
{
$params = array(
'code_number' => $this->input->post('code_number'),
'product_name' => $this->input->post('product_name'),
'brand_name' => $this->input->post('brand_name'),
'product_category' => $this->input->post('product_category'),
'cost_price' => $this->input->post('cost_price'),
'selling_price' => $this->input->post('selling_price'),
'initial_stock' => $this->input->post('initial_stock'),
);

$product_id = $this->Product_model->add_product($params);
redirect('product/index');
}
else
{
$this->load->view('product/add');
}
}


<?php echo form_open( 'product/add',array( "class"=>"form-horizontal","id"=>"dynamic")); ?>
<table>
<tbody id="itemlist">
<tr>
<td>
<input class="form-control input-sm text-right" name="code_number[]" value="<?php echo $this->input->post('code_number'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_name[]" value="<?php echo $this->input->post('product_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="brand_name[]" value="<?php echo $this->input->post('brand_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_category[]" value="<?php echo $this->input->post('product_category'); ?>">
</td>
<td>
<input class="form-control input-sm" name="cost_price[]" value="<?php echo $this->input->post('cost_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="selling_price[]" value="<?php echo $this->input->post('selling_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="initial_stock[]" value="<?php echo $this->input->post('initial_stock'); ?>">
</td>

<td class="text-center">
<button class="btn-remove btn btn-sm btn-danger"><i class="fa fa-times fa-fw"></i>
</button>
</td>
</tr><tr>
<td>
<input class="form-control input-sm text-right" name="code_number[]" value="<?php echo $this->input->post('code_number'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_name[]" value="<?php echo $this->input->post('product_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="brand_name[]" value="<?php echo $this->input->post('brand_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_category[]" value="<?php echo $this->input->post('product_category'); ?>">
</td>
<td>
<input class="form-control input-sm" name="cost_price[]" value="<?php echo $this->input->post('cost_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="selling_price[]" value="<?php echo $this->input->post('selling_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="initial_stock[]" value="<?php echo $this->input->post('initial_stock'); ?>">
</td>

<td class="text-center">
<button class="btn-remove btn btn-sm btn-danger"><i class="fa fa-times fa-fw"></i>
</button>
</td>
</tr><tr>
<td>
<input class="form-control input-sm text-right" name="code_number[]" value="<?php echo $this->input->post('code_number'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_name[]" value="<?php echo $this->input->post('product_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="brand_name[]" value="<?php echo $this->input->post('brand_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_category[]" value="<?php echo $this->input->post('product_category'); ?>">
</td>
<td>
<input class="form-control input-sm" name="cost_price[]" value="<?php echo $this->input->post('cost_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="selling_price[]" value="<?php echo $this->input->post('selling_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="initial_stock[]" value="<?php echo $this->input->post('initial_stock'); ?>">
</td>

<td class="text-center">
<button class="btn-remove btn btn-sm btn-danger"><i class="fa fa-times fa-fw"></i>
</button>
</td>
</tr><tr>
<td>
<input class="form-control input-sm text-right" name="code_number[]" value="<?php echo $this->input->post('code_number'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_name[]" value="<?php echo $this->input->post('product_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="brand_name[]" value="<?php echo $this->input->post('brand_name'); ?>">
</td>
<td>
<input class="form-control input-sm" name="product_category[]" value="<?php echo $this->input->post('product_category'); ?>">
</td>
<td>
<input class="form-control input-sm" name="cost_price[]" value="<?php echo $this->input->post('cost_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="selling_price[]" value="<?php echo $this->input->post('selling_price'); ?>">
</td>
<td>
<input class="form-control input-sm" name="initial_stock[]" value="<?php echo $this->input->post('initial_stock'); ?>">
</td>

<td class="text-center">
<button class="btn-remove btn btn-sm btn-danger"><i class="fa fa-times fa-fw"></i>
</button>
</td>
</tr>

</tbody>
</table>



Answer

Since ur already using Jquery according to ur question you should search up Ajax too. It's a nice way of posting data over ajax and will not have to refresh the page. below is a small example code for posting data to your controller.

var postData = {
    product_name1: $("#product_name1").val()
}

$.post( "/route/to/post/to", { postData  }, function( data ) {
  console.log( data); // Response data
}, "json");

You just got to build up the above array to your liking and change the route it should post to accordingly. Once you've done that you should be ready to go.

AJAX

Comments