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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算