duangan6797 2015-04-22 04:12
浏览 304
已采纳

Laravel形式一次性插入

I created a normal HTML form with 10+ fields. One of the fields is the submit button (obviously nobody wants to store it in database). I'm trying to save data in the table with this statement in the Product model:

In controller:

$id = Product::saveFormData(Input::all());

In model:

$id = DB::table('products')->insertGetId($data);

It is giving me the following error:

Column not found: 1054 Unknown column 'btncreateproduct' in 'field list' (SQL: insert into products (name, url_key, sku, quantity, price, specialprice, description, preorder, page_title, header_data, custom_json_data, btncreateproduct, category_id) values (abc, aaa, bbb, 11, 2222, 1111, abcasdf, no, asdf, asdf, asdf, Create Product, 2))'

Now, first of all, why it is considering btncreateproduct as a field. I do not want to specify the list of fields in my model as I want this code to be scalable (user can add any number of fields).

Secondly, the values abc, aaa are not like 'abc', 'aaa'

Which means at the current moment, it is not considering them as string values. Although they are defined varchar in the database.

  • 写回答

2条回答 默认 最新

  • dpno17028 2015-04-22 06:09
    关注

    About the button

    The reason why your button is part of your submission is that you put the attribute name.

    See here for reference.

    You can test with this:

    <?php var_dump($_POST); ?>
    <form action="index.php" method="post" id="form1">
        <button type="submit" name="test" value="Submit1">Submit A</button>
        <button type="submit" value="Submit2">Submit B</button>
    </form>
    

    About creating entries in the db

    What you are trying to do in your code is called Massive Assignment and is really dangerous and insecure.

    So as suggested you should use your model using the Product::create().
    In addition to that in the Model Product you have to insert the property $fillable.

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Product extends Model {
        // this is just an example, modify this part in accord to your needs
        protected $fillable = [`name`, `url_key`, `sku`, `quantity`, `price`, `specialprice`, `description`, `preorder`, `page_title`, `header_data`, `custom_json_data`, `category_id`];
    
        // other code here 
    }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?