dthjnc306679 2017-05-21 10:45
浏览 101

更新日期时间最后8个字符(... -..- .. hh:mm:ss)

I have datetime column which is in this format YYYY-MM-DD HH:mm:ss.

But in my php form for updating this data I have created 2 separate fields one is for DATEpart and one is for TIMEpart.

There is no problem to receive/load these values into my php form from table using:

RIGHT(datetime,8) for TIMEpart or for DATEpart LEFT(datetime,10)

Problem is I am not able to update also only that part of data. If I am attempting to update with TIMEpart it will try to update first 8 characters of datetime instead of attempting to update last 8 character. Problem is I do not know how to tell the program to do so, I know that it needs to be defined into my sql query.

$query3=mysqli_query($db,"update LISTS set datetime=LEFT(datetime,10), 
datetime=RIGHT(datetime,8) where id='$id'");

I am new into SQL and PHP. I have this already working by having 2 separate columns but I would like to simplify sql table if possible.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanmei1885 2017-05-21 11:04
    关注

    I think you want to use addtime():

    update lists
        set datetime = addtime(@date, @time)
        where id = @id;
    

    Learn to use proper parameterized queries rather than munging the query string with user input values. Your approach is likely to lead to inexplicable syntax errors and makes the code vulnerable to SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?