douzhi2760 2011-07-19 04:20
浏览 128
已采纳

删除或更新时再次填充NULL值

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 condition if $photo is empty it will fill blank value of $a... Let me know to fill NULL in database.

  • 写回答

1条回答 默认 最新

  • douna3367 2011-07-19 04:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)