简体   繁体   English

如何将空值从HTML表单传递到mysql db外键字段

[英]How to pass a null value from HTML form to mysql db foreign key field

I have two tables: parent and child. 我有两个表:父母和孩子。 One of the child column references parent PRIMARY KEY with FOREIGN KEY constraint - child.parent_id references parent.parent_id. 子列之一引用具有FOREIGN KEY约束的父PRIMARY KEY-child.parent_id引用parent.parent_id。

Foreign key column in child tables allows NULLs values because when child will be created parent may be not known yet. 子表中的外键列允许使用NULL值,因为创建子项的时间可能未知。 But when info about parent will be assigned/updated i want to have referential integrity. 但是,当有关父项的信息将被分配/更新时,我希望具有参照完整性。

And now the question: 现在的问题是:

How to pass null value from HTML form to MySQL? 如何将空值从HTML表单传递给MySQL? Quotes '' or "" not working "foreign key constraint fails" probably because it passes empty string instead of null value. 用引号''或“”无效“外键约束失败”,可能是因为它传递了空字符串而不是空值。 Or maybe do I need to do some additional checking in PHP and convert it to null ? 或者,也许我需要在PHP中进行一些附加检查并将其转换为null?

CREATE TABLE IF NOT EXISTS `parent` (
  `parent_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `surname` varchar(20) NOT NULL,
  PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `child` (
 `child_id` int(11) NOT NULL AUTO_INCREMENT,
 `child_name` varchar(20) NOT NULL,
 `parent_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`child_id`),
 KEY `fk_parent_id` (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `child`
  ADD CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`parent_id`);

您需要将空值转换为null或在PHP中编辑查询。

Why do you want to pass up a NULL value when your tables do not support NULL values in the parent? 为什么你想传递一个NULL值时,你的表不支持NULL父值? Also

`parent_id` int(11) DEFAULT NULL,

Should be NOT NULL as a child should always have a parent. 应该NOT NULL因为孩子应该始终有一个父母。

If you are trying to pass parent/child identifiers from a HTML form, I would recommend validating them server side in your PHP code. 如果您尝试通过HTML表单传递父/子标识符,我建议在您的PHP代码中在服务器端对其进行验证。 As I could easily manipulate the values in the markup and add any parent/child combination I wish. 因为我可以轻松地操纵标记中的值并添加我希望的任何父/子组合。

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

相关问题 如何使用外键将PHP表单中的数据插入到MySQL数据库中? - How to insert data from a PHP form into a MySQL db with Foreign keys? 如何将值的字段而不是表单的名称字段传递给Mysql - how to pass the value field instead of the name field of a form to Mysql 如何将已经在数据库中的 ID 传递给新的 HTML 表单并将其作为 PHP 中的外键传递给数据库 - How to pass the ID which is already in the database,to a new HTML form and pass it to the database as the foreign key in PHP 如何使用php和mysql将外键字段链接到另一个表的主键字段? - How to link the foreign key field to a primary key field from another table using php and mysql? MySql Update并将null设置为具有其他表的外键的字段 - MySql Update and Set null to field with foreign key to other table MySQL使用父键更新外键字段值 - mysql update values in foreign key field value with parent key 如何在php的外键上允许NULL值 - How to allow NULL value on foreign key on php 如何接受html格式的浮点值并将其发送到MySQL DB - How to accept a floating point value in an html form and send it to MySQL DB 如何将值从Jquery传递给PHP? HTML是隐藏的输入表单字段 - How do I pass value from Jquery to PHP ? HTML is a hidden input form field 如何通过外键获取字段的值 - how to get the value of field by foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM