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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?