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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作