I have updated certain field in my database & that field has default value as NULL. How can I again fill NULL if field is emptied or deleted. As during this updation blank value is filled in database and of course that is not NULL. Is filling NULL instead of blank value good?
Using php how can I do that? Suppose I have condition if code
if(!empty($photo))
{ $a in database;
}
But in this开发者_JAVA技巧 condition if $photo
is empty it will fill blank value of $a
... Let me know to fill NULL in database.
You just have to write null
, and not an empty value, to your database.
You SQL query would look like this :
update your_table
set your_field = NULL
where ...
instead of :
update your_table
set your_field = ''
where ...
To switch between those two queries, you might need some condition in your PHP code, depending on how it's organized ; maybe like this :
if (empty($photo)) {
// query to set the field to NULL
}
else {
// query to update the value
}
Note that if you have no value to store for a row in your database, you might also want to just delete that row :
delete from your_table
where ...
Of course, it's up to you to determine which is the best for your application : a row with a NULL value, or no row.
精彩评论