比较java历元字符串和php历元日期
这里有一个java字符串epoch date,我想比较并向其中添加数据 SQL它给出了一个解析错误,不知道为什么会有任何解决方案比较java历元字符串和php历元日期,java,php,android,mysql,Java,Php,Android,Mysql,这里有一个java字符串epoch date,我想比较并向其中添加数据 SQL它给出了一个解析错误,不知道为什么会有任何解决方案 <?php if($_SERVER['REQUEST_METHOD']=='POST'){ //Getting values $number = $_POST['number']; $type = $_POST['type']; $date = $_POST['date']; $content = $_POST['content']; $start = strt
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$number = $_POST['number'];
$type = $_POST['type'];
$date = $_POST['date'];
$content = $_POST['content'];
$start = strtotime("now");
$end = strtotime("-7 days");
while($start->format('U') > $date->format('U') > $end->format('U')){
$sql = "INSERT INTO message_detail (number,type,date,content) VALUES ('$number','$type','$date','$content')";
//Importing our db connection script
require_once('connect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Entry Added Successfully';
}else{
echo 'Could Not Add Entry';
}
}
//Closing the database
mysqli_close($con);
}
?>
您的代码几乎没有问题,例如:
- 函数返回一个整数时间戳,而不是一个对象,所以不能这样调用
format()
方法
$start = strtotime("now");
$start->format('U');
// etc. are wrong
相反,创建一个DateTime
对象,然后调用它的format()
方法,如下所示:
$start = new DateTime("now");
$start->format('U');
// etc.
while($start->format('U') < $date->format('U') && $date->format('U') > $end->format('U')){
// your code
}
- 现在谈到你的问题
它给出了一个解析错误
那是因为你的
while
状况
while($start->format('U') > $date->format('U') > $end->format('U')){ ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
也许,你想做这样的事情:
$start = new DateTime("now");
$start->format('U');
// etc.
while($start->format('U') < $date->format('U') && $date->format('U') > $end->format('U')){
// your code
}
使用while
循环没有意义,一个简单的if
条件就可以了
if($start <= $date && $end >= $date){
// your code
}
if($start->format('U') <= $date->format('U') && $end->format('U') >= $date->format('U')){
// your code
}
使用while
循环没有意义,一个简单的if
条件就可以了
if($start <= $date && $end >= $date){
// your code
}
if($start->format('U') <= $date->format('U') && $end->format('U') >= $date->format('U')){
// your code
}
if($start->format('U')format('U')&&&$end->format('U')>=$date->format('U')){
//你的代码
}
在我看来,您的代码存在一些问题。首先,您将db连接脚本包含在一个循环中,其次,您将直接在sql中嵌入变量,从而使其容易受到sql注入攻击。感谢您的回答,但我只想输入从输入时间算起的最后7天的数据,因此我必须使用strotime()还有其他方法吗…??@HirenGujarati我已经编辑了我的答案。请参阅我答案的编辑部分。hey@rajdeep如果(mysqli_query($con,$sql)){在这一行称为mysqli_query():第19行的/home/u336100496/public\u html/call\u detail/insert.php中的空查询
无法成功添加EntryConnected
@HirenGujarati您是否在if
块中构造了查询,如下所示:$sql=“insert INTO message\u detail…”
?我不这么认为。