简体   繁体   中英

php INSERT mysql 5.7

I use to insert values in php like this:

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

considering that my table has the following columns: 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.

I add a default value for it on mysql, but some fields dont allow default value, like 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 .

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).

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).

ALTER TABLE table_name 
CHANGE column_name column_name type DEFAULT NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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