php - 谁能帮我找到php中基于日期范围的插入查询的问题

标签 php mysql

我从复选框获取日期范围和多个 id (chkarray) ids 和日期范围变得很好,循环也完美执行,但我的 mysql 查询没有被执行。

<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(E_ALL);
include ('db.php'); 
    $date_start = $_POST['date_start'];
    $date_end = $_POST['date_end'];
    $chkarray = $_POST['chk'];
    $shift = $_POST['shiftTime'];
    if(!empty($date_start)){
    echo "Start date: $date_start"."<br>";
    echo '<pre>';
print_r($_POST);
echo '</pre>';
$i=0;

while ($date_start<=$date_end){
  echo "<br>".$i."<br>".$date_start."<br>";
  foreach ($chkarray as $val) {  
    echo $val."  ";
$new_date = $date_start;
$sql1=mysql_fetch_assoc(mysql_query("Select * FROM user_shift_test WHERE userid='$val' ",$conn));
$e_id=$sql1['id'];
$e_name=$sql1['name'];
$e_dayoff=$sql1['dayoff'];
$query = mysql_query("INSERT INTO users_new_test (userid,id,name,shift,date_start,date_end,dayoff,updatetime) VALUES ('$val','$e_id','$e_name','$shift','$new_date','$date_end','$e_dayoff',now())");
if($sql1){
echo "Success";
}
else{echo "ERROR!!!!<br>";}

每次打印错误行

}$date_start = date ("Y-m-d", strtotime("+1 day", strtotime($date_start)));
$i++;
}
echo "<br>Successfully Inserted";
}//first if closing
else{
echo "Nothing selected";
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>

最佳答案

它工作正常。我把它改成了mysqli

<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(E_ALL);
include ('db.php'); 
    $date_start = $_POST['date_start'];
    $date_end = $_POST['date_end'];
    $chkarray = $_POST['chk'];
    $shift = $_POST['shiftTime'];
if(!empty($date_start)){
echo "Start date: $date_start"."<br>";
echo '<pre>';
print_r($_POST);
echo '</pre>';
$i=0;

while ($date_start<=$date_end){
echo "<br>".$i."<br>".$date_start."<br>";
 foreach ($chkarray as $val) {  
    echo $val."  ";
 $conn = mysqli_connect($servername,$database_username,$database_password,$database_name);
$new_date = $date_start;
$sql1=mysqli_fetch_assoc(mysqli_query($conn,"Select * FROM user_shift_test WHERE userid='$val' "));
echo $e_id=$sql1['id'];
echo $e_name=$sql1['name'];
echo $e_dayoff=$sql1['dayoff'];
$query = mysqli_query($conn,"INSERT INTO users_new_test (userid,id,name,shift,date_start,date_end,dayoff,updatetime) VALUES ('$val','$e_id','$e_name','$shift','$new_date','$date_end','$e_dayoff',now())");
if($sql1){
echo "Success";
}
else{echo "ERROR!!!!<br>";}

现在工作正常了。错误现在消失了

}$date_start = date ("Y-m-d", strtotime("+1 day", strtotime($date_start)));
$i++;
}
echo "<br>Successfully Inserted";
}//first if closing
else{
echo "Nothing selected";
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>

关于php - 谁能帮我找到php中基于日期范围的插入查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39140863/

相关文章:

javascript - Codeigniter 中不同 Controller 的不同 JavaScript、JQuery 或 CSS

PHP sql 查询 WHERE 值 = 0

php - 在 MongoDB 中查找字段不为空的字段(在 PHP 中)

php - SQL 计算具有匹配值的行

android - 适用于 Android 应用程序的 RESTful Web 服务

php - mysql查询中的距离计算

php - 在 PHP mail() 函数中使用特殊字符

mysql - 与结束/反向偏移的偏移?

php - 显示数据库内容? PHP/MySQL

php - 多个 PHP MySQL 更简单的查询与一个更复杂的查询,