douchensou6495 2013-04-08 22:06
浏览 578
已采纳

更新日期字段时为空 - MySQL PHP

I'm trying to update date field in my db. Query like this:

$q = "update news set data = STR_TO_DATE('2011-03-05','%Y-%m-%d'), title = '".$title."', content='".$content."',....";

works great, but:

$q = "update news set data = STR_TO_DATE('".$data."','%Y-%m-%d'), title = '".$title."', content='"...";

it is not working :(

I got date:

$data = $_POST["data"];

and it has a value "2013-04-13". I trimmed date and show in popup window and value is correct; Plz help :)

UPDATE It is strange to me, but if i'm using:

$q = "insert into news set data = CAST('".$data."' AS DATE), title = '".$title."', content='".$content."'...";

it works perfectly fine. Only in insert not in update

Script for table:

CREATE TABLE IF NOT EXISTS `news` (`id` int(11) NOT NULL AUTO_INCREMENT, `data` date NOT NULL, `title` text NOT NULL, `content` text NOT NULL, `img` text NOT NULL, `n_img` text NOT NULL, PRIMARY KEY (`id`), KEY `id` (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=36 ;
  • 写回答

1条回答 默认 最新

  • dongqian7545 2013-04-08 22:13
    关注

    why are you using STR_TO_DATE? looks like your date is already in the correct format. try removing it and just inserting as is?

    might want to escape it first though.

    $data = mysql_real_escape_string($_POST["data"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题