2016-02-11 80 views
0

我使用PHP编写了一个使用PHP & MySQL的新闻网站。我想以d-m-Y格式显示日期。为此,我已经使用:以d-m-Y格式显示MySQL数据库的日期

$date='<td>'.$row['date'].'</td>'; //'date' is the value from the database 
$newdate=strtotime($date);  

$showdate=date("d-m-Y",$newdate); 

     echo $showdate; 

但是,我只是得到日期为1-1-1970所有提取。问题是什么?

+0

哪里是两个贪利 –

+0

把这个'行动=“insert.php”'在第二个形式,否则将无法正常工作 –

+0

特殊字符在服务器端逃逸是处理黑客的好方法,逃避一切从客户端到服务器的特殊字符。这可以保护你免受黑客的侵害。请参阅以下链接以获取更多信息[跨站点脚本](https://en.wikipedia.org/wiki/Cross-site_scripting) –

回答

1

这不仅仅是为了避免黑客入侵。您还需要检查来自后端(PHP)的用户输入。

以下是有关验证用户输入的文章:https://www.dreamhost.com/blog/2013/05/22/php-security-user-validation-and-sanitization-for-the-beginner/

您还可以阅读有关SQL注入:http://php.net/manual/en/security.database.sql-injection.php

关于形式action属性:http://www.sitepoint.com/web-foundations/action-html-attribute/

现在关于形式方法:

定义及用法

formmethod属性定义了将表单数据 发送到操作URL的HTTP方法。

formmethod属性重写 元素的方法属性。

注意:formmethod属性可以与type =“submit”和 type =“image”一起使用。

表单数据可以作为URL变量(method =“get”)发送,也可以作为HTTP post事务(method =“post”)发送。

的“get”方法注:

This method appends the form-data to the URL in name/value pairs 
This method is useful for form submissions where a user want to bookmark the result 
There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the 

形式的数据将是正确传递 ,切勿使用“get”方法来传递敏感信息! (密码或其他敏感信息将在 浏览器的地址栏中显示)

注意事项“后”的方法:

This method sends the form-data as an HTTP post transaction 
Form submissions with the "post" method cannot be bookmarked 
The "post" method is more robust and secure than "get", and "post" does not have size limitations 

来源:http://www.w3schools.com/tags/att_input_formmethod.asp

0

都没有。 从一种形式的数据传递到你的PHP脚本,你只需要创建一个响应脚本过滤等领域,可以使用strip_tagsfilter_var

的Html

<form action="myscript.php" method="post"> 
     <!-- your values --> 
    </form> 

你的PHP脚本

if(isset($_POST['your_input_name'])): 
    $name = strip_tags($_POST['your_input_name']); 
    $name = filter_var($name, FILTER_SANITIZE_STRING); 
    # Do something with your input.. 
endif; 
0

你的代码使用几个不同的日期字段,你不确定它是哪一天来自数据库。不过,我想我可以用一个可以应用于您的案例的通用示例来回答这个问题。对于这个答案,我们将假设你有一个名为$ date的变量来存储数据库中的值。

echo date('d-m-Y', strtotime($date)); 
+0

我已经显示了正确的字段。我也使用了你的建议,但是我仍然以1-1-1970为输出。 –

+0

你能提供一个来自数据库的日期字段原始数据的例子吗?这可能会帮助我回答你。谢谢。 – colonelclick

0

正确的代码:

$ DBDATE = $行[ '日期']; [这将持有从MySql数据库的日期。]

$ newdate = strtotime($ dbdate);

$ showdate = date(“d-m-Y”,$ newdate);

echo $showdate; 

我得到了正确的结果。

+0

很高兴听到它。你的工作代码与我上面的答案相同。正如你所看到的,我将数据库中的日期传递给strtotime,然后用d-m-Y和date命令对其进行格式化。 – colonelclick

0

请添加为像following..here首先检查该珍惜你从数据库中$行[“日期”]中得到。如果你在这里约会,然后使用以下行DMY格式

转换
$date=date_create($row['date']); // but here first check value of $row['date'] 
echo date_format($date,"d-m-Y"); 
+1

你能否解释你的答案如何解决问题中的问题?仅有代码的答案并不是很有用,特别是对于那些偶然发现这篇文章的读者。谢谢! – Cristik