简体   繁体   English

php INSERT mysql 5.7

[英]php INSERT mysql 5.7

I use to insert values in php like this: 我用来在php中插入值,如下所示:

$sql = " INSERT INTO `table` (name, email) VALUES ('$name','$email') ";

considering that my table has the following columns: id, name, email, address, phone 考虑到我的表格包含以下列:id,name,email,address,phone

in mysql 5.7 it won't let my do that query above anymore, cause it's says address don't have a default value. 在mysql 5.7中,它不会让我再进行上面的查询,因为它说地址没有默认值。

I add a default value for it on mysql, but some fields dont allow default value, like blob, text... 我在mysql上添加了一个默认值,但有些字段不允许默认值,如blob,text ...

my question is: Do I always have to describe every column of my table in my query so it can work? 我的问题是:我是否总是要在我的查询中描述我的表的每一列,以便它可以工作? Like: 喜欢:

$sql = " INSERT INTO `table` (id,name, email, address, phone) 
VALUES ('', '$name','$email', '', '') ";

You don't have to describe every column for every query you make, but particularly for INSERT queries, you have to make sure that the columns you don't specify either have a default value, or can be NULL . 您不必为每个查询描述每个列,但特别是对于INSERT查询,您必须确保指定的列具有默认值,或者可以为NULL

For other queries, such as SELECT or UPDATE , you can choose just the ones you want without regard what the content is (of course same applies for UPDATE as INSERT , that columns which cannot be NULL, can't have a NULL-value in it). 对于其他查询,例如SELECTUPDATE ,您可以选择您想要的那些而不考虑内容是什么(当然同样适用于UPDATE作为INSERT ,那些不能为NULL的列,不能具有NULL值它)。

You can alter your table such that the values you don't always insert either have a default value, or just set them to NULL by default (if you don't supply a value for that column upon inserting). 您可以更改表,使您不总是插入的值具有默认值,或者默认情况下将它们设置为NULL(如果在插入时没有为该列提供值)。

ALTER TABLE table_name 
CHANGE column_name column_name type DEFAULT NULL

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM