0

这是我的变量:

     <?php 
     //...
     $creat='2000-03-15'  ,from an input field.
     ?> 

这是我的插入代码:

     <?php 
     //...
       $this->Comment->updateAll(         
              array('Comment.c' => $creat ),
              array('Comment.id' => '3'  ));
     ?> 

当我想将它保存在具有日期字段类型而不是“2000-03-15”的表中时,我有“0000-00-00”?
为什么?
感谢您的评论!

4

1 回答 1

0

updateAll 函数不会转义您的输入。它也不会将您的数据用引号括起来。

在您的示例中,要正确转义您的 $creat 变量,我建议使用以下代码:

<?php
App::uses('Sanitize', 'Utility');
$Comment->updateAll(array(
    'Comment.c' => "'" . Sanitize::escape($creat) . "'"
), array(
    'Comment.id' => 3
));

我们用引号包裹日期,并通过 Sanitize::escape 传递它,以确保我们保护自己免受 SQL 注入。

于 2012-05-19T15:55:24.177 回答