以html格式将空字符串更新为NULL [英] Update empty string to NULL in a html form

查看:276
本文介绍了以html格式将空字符串更新为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel中建立一个站点.

I'm building a site in Laravel.

我在InnoDB表之间设置了外键约束.

I have foreign key constraints set up among InnoDB tables.

我的问题是,如果我没有在选择"框中选择一个值,框架将尝试在表中插入或更新带有(空字符串)"的记录.这会导致MySQL错误,因为它无法在子表中找到等效的外键值.

My problem is that if i don't select a value in a, say, select box, the framework tries to insert or update a record in a table with '' (empty string). Which causes a MySQL error as it cannot find the equivalent foreign key value in the subtables.

除了签出每个字段外,是否有某种优雅的方法可以在外键字段中强制插入NULL?还是要强制MySQL接受''作为空"外键引用?

Is there some elegant way to force the insertion of NULL in the foreign key fields other than checking out every single field? Or to force MySQL to accept '' as a "null" foreign key reference?

换句话说:我有一个SELECT字段,第一个OPTION为空白.我保留选择的空白选项.当我提交时,传递了一个空字符串.在MySQL中,我显然可以执行UPDATE table SET foreignKey = NULL,但不能执行UPDATE table SET foreignKey =''.它不会转换"为NULL.我可以一一检查这些字段,然后为每个外键将''转换为NULL,也许在数组中指定所有这些外键,我想知道是否有更简化的方法来做到这一点.

In other words: I have a, say, SELECT field with first OPTION blank. I leave the blank OPTION chosen. When I submit, an empty string '' is passed. In MySQL apparently I can do UPDATE table SET foreignKey=NULL but not UPDATE table SET foreignKey=''. It does not "convert" to NULL. I could check the fields one by one but and convert '' to NULL for every foreign key, maybe specifying all of them in an array, I was wondering if there's a more streamlined way to do this.

也许必须在我的数据库模式中更改我的ON UPDATE操作(未设置)?

Maybe have to change my ON UPDATE action (which is not set) in my DB schema?

列确实接受NULL值,问题在于框架或MySQL如何处理来自HTML的空值".我不是在建议MySQL做错了",这也是合乎逻辑的,问题是您不能在HTML中设置"NULL"值,我想知道是否有一种优雅的方法可以解决此问题. MySQL Laravel.

the columns DO accept the NULL value, the problem is in how the framework or MySQL handle the "empty value" coming from the HTML. I'm not suggesting MySQL "does it wrong", it is also logical, the problem is that you can't set a "NULL" value in HTML, and I would like to know if there's an elegant way to manage this problem in MySQL or Laravel.

换句话说,我是否必须手动指定外键并相应地构造查询?还是有另一种健壮而优雅的方法?

In other words, do I have to specify manually the foreign keys and construct my query accordingly or is there another robust and elegant way?

到目前为止,我对模型MyModel的代码:

My code so far for the model MyModel:

$obj = new MyModel;
$obj->fill(Input::all())); // can be all() or a subset of the request fields
$obj->save();

推荐答案

代替使用

$obj = new MyModel;
$obj->fill(Input::all())); // can be all() or a subset of the request fields
$obj->save();

使用

$obj = new MyModel;
$obj->fieldName1 = Input::get('formField1');
$obj->fieldName2 = Input::has('formField2') && Input::get('formField2') == 'someValue' ? Input::get('formField2') :  null;
// ...
$obj->save();

并确保您的数据库field接受null值.另外,您可以将database/phpmyadmin中的默认值设置为null.

And make sure your database field accepts null values. Also, you can set a default value as null from the database/phpmyadmin.

这篇关于以html格式将空字符串更新为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆