dqaq59269 2016-11-06 12:14
浏览 41
已采纳

发生数据库错误如何在codeigniter中使用jQuery提交相同名称的输入字段

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>
  • 写回答

1条回答 默认 最新

  • doulu1325 2016-11-06 13:12
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失