0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ashutosh
  • 4,371
  • 10
  • 59
  • 105

2 Answers2

4

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 
}  
Community
  • 1
  • 1
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
  • Thanks borracciaBlu it works. What I want is a scalable solution like we add custom attributes in Magento to products. Anyways, I will try to figure out some solution. – Ashutosh Apr 23 '15 at 01:36
1

For what possible reason are you using QueryBuilder DB::table('products')... instead of the model itself for saving a new product?

In any case you can do something along the lines of

$input = Input::except(['btncreateproduct']);
$newProduct = Product::create($input);
$id = $newProduct->id;

That being said you better do a proper validation and type casting and manually assign these values, because at some point either table schema or form will change and break your code anyway.

peterm
  • 91,357
  • 15
  • 148
  • 157