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"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了
  • ¥15 gazebo-rviz教程
  • ¥15 付费求做一个自助抢单插件
  • ¥15 bat批处理,关于数据复制问题
  • ¥50 同步两个不同结果的array中某些属性
  • ¥15 悬赏15远程操控解决问题
  • ¥15 CST复制的模型无法单独修改参数?
  • ¥15 前端页面想做个定时任务,但是使用requestAnimationFrame,setinterval和settimeout都不行
  • ¥15 根据以下文字信息,做EA模型图