简体   繁体   English

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

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

I am new to stackoverflow. 我是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. 我正在尝试使用Codeigniter构建一个小型应用程序。 I do not not how to pass dynamic same input field value using jQuery to Codeigniter controller to model. 我不是如何使用jQuery将动态相同的输入字段值传递给Codeigniter控制器进行建模。 I am using this jQuery plugin to make dynamic input field. 我正在使用这个jQuery插件来创建动态输入字段。

For test purpose, I have pasted same html input code more than 3times. 出于测试目的,我将相同的html输入代码粘贴了3次以上。

Error: 错误:

A Database Error Occurred 发生数据库错误

Error Number: 1064 错误号:1064

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0, 1, 2) VALUES ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('',' at line 1 检查与您的MariaDB服务器版本相对应的手册以获取正确的语法以在'0,1,2)VALUES('','',''),('','',''),('' ,'',''),('','',''),('','',''),('','在第1行

INSERT INTO products (0, 1, 2) VALUES ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('','',''), ('','','') 插入products (0,1,2)值('','',''),('','',''),('','',''),('','' ,''),('','',''),('','',''),('','','')

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

Line Number: 691 行号: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>

Since ur already using Jquery according to ur question you should search up Ajax too. 由于您已经根据您的问题使用了Jquery,因此您也应该搜索Ajax。 It's a nice way of posting data over ajax and will not have to refresh the page. 这是一种通过ajax发布数据的好方法,并且无需刷新页面。 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 AJAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Codeigniter中使用具有相同名称的多个输入插入数据 - How To Insert Data Using Multiple Input With Same Name in codeigniter 如何使用具有相同名称的jquery获取每个输入字段的值? - How to get values of each input field using jquery having same name? jQuery - 使用同时填充的另一个输入字段的值来更改输入字段的名称 - jQuery - Change name of an input field by using value of another input field filled at the same time 如何将动态输入字段提交到数据库中? - How to submit the dynamical input field into the database? 如何在相同的提交类型输入字段(按钮)中使用onclick(js函数)方法提交表单和处理事件 - How to submit form and handle events using onclick(js function) method in same submit type input field(button) jQuery如何添加名称输入submit() - How add name input submit() jQuery 使用jQuery Ajax提交按钮将数据插入数据库中的代码igniter - inserting data into database using jquery ajax submit button codeigniter 如何使用Javascript / Jquery动态添加输入字段的名称属性 - How to add name attribute of input field dynamically using Javascript/Jquery 在 codeigniter 中使用 ajax 和 jquery 验证字段时,输入组插件出错 - Getting error in input-group-addon when validating the field using ajax and jquery in codeigniter 在Codeigniter上使用jQuery提交动画 - Animation on submit using jQuery on codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM