试图通过php与mySQL发布日期...需要帮助

时间:2011-09-25 21:04:10

标签: php mysql date post

我正在尝试在创建新记录时发布日期和时间。记录已创建,但mySQL中的“add_time”列为空。

它出了什么问题?

$date = date("Y-m-d G:i:s") ; 

$order = "INSERT INTO cartons_added (add_time, type, part_no, add_type, add_qty, 
add_ref, add_by, add_notes)

VALUES
('$_POST[date]',
 '$_POST[type]', 
 '$_POST[part_no]', 
 '$_POST[add_type]', 
 '$_POST[add_qty]', 
 '$_POST[add_ref]', 
 '$_POST[add_by]', 
 '$_POST[add_notes]')";

 $result = mysql_query($order);

4 个答案:

答案 0 :(得分:2)

您永远不会使用您创建的$date变量。您可能打算使用它代替$_POST[date]

答案 1 :(得分:1)

我相信而不是:

VALUES
('$_POST[date]',
 '$_POST[type]', 
 '$_POST[part_no]', 
 '$_POST[add_type]', 
 '$_POST[add_qty]', 
 '$_POST[add_ref]', 
 '$_POST[add_by]', 
 '$_POST[add_notes]')";

你的意思是使用

// Use your $date variable

VALUES
('$date',
 '$_POST[type]', 
 '$_POST[part_no]', 
 '$_POST[add_type]', 
 '$_POST[add_qty]', 
 '$_POST[add_ref]', 
 '$_POST[add_by]', 
 '$_POST[add_notes]')";

所有这些都需要大量的处理来防止SQL注入。最简单的方法是在$_POST中包围所有mysql_real_escape_string()个变量:

"...
VALUES
('$date',
 '" . mysql_real_escape_string($_POST['type']) ."', 
 '" . mysql_real_escape_string($_POST['part_no']) ."', 
 '" . mysql_real_escape_string($_POST['add_type']) ."', 
 '" . mysql_real_escape_string($_POST['add_qty']) ."', 
 '" . mysql_real_escape_string($_POST['add_ref']) ."', 
 '" . mysql_real_escape_string($_POST['add_by']) ."', 
 '" . mysql_real_escape_string($_POST['add_notes']) ."')";

答案 2 :(得分:0)

试试这个:

date('Y-m-d H:i:s');

答案 3 :(得分:0)

你必须修复那个SQL注入漏洞:
还有一个语法错误,它不是$_POST[add_ref],而是$_POST['add_ref']
你可以写'$_POST[name]' (差)而不是$_POST['name'](好),但这不是不好的做法。

将代码更改为:

$query = "INSERT INTO cartons_added (add_time, type, part_no, add_type, add_qty, 
                                     add_ref, add_by, add_notes)
    VALUES
    ('$date',
     '{mysql_real_escape_string($_POST['type'])}', 
     '{mysql_real_escape_string($_POST['part_no'])}', 
     '{mysql_real_escape_string($_POST['add_type'])}', 
     '{mysql_real_escape_string($_POST['add_qty'])}', 
     '{mysql_real_escape_string($_POST['add_ref'])}', 
     '{mysql_real_escape_string($_POST['add_by'])}', 
     '{mysql_real_escape_string($_POST['add_notes'])}') ";

永远不要将$_POST$_GET$_SESSION等相似内容直接插入查询中。
请参阅:How does the SQL injection from the "Bobby Tables" XKCD comic work?