简体   繁体   English

插入空数据MySQL的最佳方法是什么

[英]What is the best way to insert Null Data Mysql

I'm looking on my project to create an entry on my database wuth PHP and MySQL. 我正在寻找项目,以在数据库中使用PHP和MySQL创建一个条目。 I've created an class "Order" and iside this class I have a function to create a new entry in my Database. 我创建了一个“ Order”类,并且在该类的基础上,我具有一个在数据库中创建新条目的功能。

On my code I want to reuse this function, but hte fact is, when i create the entry I haven't all the Datas (Some Datas are inserted later in the process). 在我的代码上,我想重用此函数,但事实是,当我创建条目时,我并没有所有数据(某些数据会在此过程的后面插入)。 So my question is the next one. 所以我的问题是下一个。 What is the best to manage it ? 什么是最好的管理方法?

Should I generate some data as NULL ? 我应该将某些数据生成为NULL吗? (that's my fort idea) Like that ? (那是我的要塞)那样吗?

 $unknow = NULL;
 $end_date = date('d/m/Y', strtotime("+1 month"));
 $user = checkConnected($bdd);
 Order::create($bdd, $new_full_modele_devis_path, $unknow, $end_date, 0, $user->getId(), $unknow, $entreprise->getId());

or should I rework my finction to ignore empty datas ? 还是应该重新整理功能以忽略空数据?

Thank you for your answers. 谢谢您的回答。

Let database fill the NULL values. 让数据库填充NULL值。 Make those fields nullable in database which you want to insert later. 使这些字段在您以后要插入的数据库中为空。

When you make your database structure you can set default values of your fields. 建立数据库结构时,可以设置字段的默认值。 In your case if a field may not be filled in you can set the default value as NULL meaning you don't need to touch that field in your code until you want to save a value to it. 在您的情况下,如果可能未填写字段,则可以将默认值设置为NULL这意味着您无需在代码中触摸该字段,除非您想向其中保存一个值。

It is not a good practice to save the null values. 保存空值不是一个好习惯。 Instead you set the default values so that when there is null value then it would save default value. 而是设置默认值,以便当存在空值时它将保存默认值。

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

相关问题 将帖子、标签、类别插入 mysql 的最佳方法是什么? - What is best way to insert post,tag,category to mysql? 在一个查询中在MySQL中插入/更新的最佳方法是什么? - What's the best way to do an insert/update in MySQL in one query? 在PHP PDO MYSQL中插入多行的最佳方法是什么? - What is the best way to insert multiple rows in PHP PDO MYSQL? 将多个表单数组插入MySQL数据库的最佳方法是什么? - What is the best way to insert multiple form arrays into a MySQL database? 对我来说,将数据插入表格提交表中的最佳方法是什么? - What would be the best way for me to insert data to table on form submit? 在 MySQL 中构建和存储部分数据的最佳方法是什么? - What's the best way to structure and store part of data in MySQL? 将html数据存储在mysql数据库中的最佳方法是什么? - What's the Best way to store html data in a mysql database? 在 mysql 中存储关注、关注、喜欢数据的最佳方式是什么 - What is the best way of storing following,followers,likes data in mysql 考虑到速度和安全性,从MySQL调用数据的最佳方法是什么? - what is the best way to call data from MySQL with speed and security in mind? 在MySQL中存储敏感数据的最佳方法是什么? - What's the best way to store sensitive data in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM