我有一个如上所述的 MySQL 数据库,除了 UID 之外,所有 Null 都设置为 Yes。数据将从调查中收集,当所有条目都符合 MySQL 定义的“类型”时,数据才会起作用。
但有一个问题,有时用户可能会输入不符合条件的内容,例如:“用户在 age 中输入 varchar(4) 而不是 int(3)”。
现在发生的情况是,由于该单个错误,整行将不会被插入。我想做的是有一种方法,这样只有 age 的条目会被省略。我以为将 Null 设置为 Yes 可以解决问题,但显然不能。请帮忙:)
最佳答案
ALLOW NULL
并不意味着“如果无效,则插入 NULL”。您应该在插入之前验证数据。
您可以使用IGNORE像这样插入时的关键字:
INSERT IGNORE INTO tbl
它提供以下功能:
Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. You can determine with the mysql_info() C API function how many rows were actually inserted into the table.
因此数据转换错误不会中止,但约束错误会像重复的唯一键或主键一样中止。
关于php - 将行插入 mysql 时忽略错误和不匹配类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9760531/