【问题标题】:How to select my selected customer orders in sql between specific dates [duplicate]如何在特定日期之间在sql中选择我选择的客户订单[重复]
【发布时间】:2020-11-19 10:14:36
【问题描述】:

您好,我想列出所选日期之间的所选客户发票。

我已经创建了一个表单

<form action="raporlar.php" method="POST">
  <div class="col-md-3">
    <label for="">Customer Name</label>
    <select name="customerID" id="customerID" required="" class="form-control">
      <option value="">Select a customer</option>
      <?php 
      $musteriSor=$db->prepare('SELECT * FROM customer'); 
      $musteriSor->execute();
      while ($musteriCek=$musteriSor->fetch(PDO::FETCH_ASSOC)) { ?>
      <option value="<?php echo $musteriCek['ID']; ?>"><?php echo $musteriCek['adi']; ?></option>
      <?php } ?>
    </select>
  </div>
  <div class="col-md-7">
    <div class="row">
      <div class="col-md-6">
        <label for="">First Date</label>
        <input type="date" name="firstDate" required="" class="form-control">
      </div>
      <div class="col-md-6">
        <label for="">Second Date</label>
        <input type="date" name="secondDate" required="" class="form-control">
      </div>
    </div>
  </div>
  <div class="col-md-2">
    <input type="submit" name="raporla" value="Raporla" class="btn">
  </div>
</form>

这是我的 sql 查询

<?php 
$queryim = 'SELECT * FROM invoice WHERE customerID=:customerID and date=:date BETWEEN '.$_POST['firstDate'].' AND '.$_POST['secondDate'].'';
$faturaSor=$db->prepare($queryim); 
$faturaSor->execute(array('customerID'=>$_POST['customerID']));
while ($faturaCek=$faturaSor->fetch(PDO::FETCH_ASSOC)) { 
    ... 
}
?>

它给出了这个错误 -> 警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: 绑定变量的数量与 /test.local 中的标记数量不匹配/raporlar.php 第 89 行

如何列出选定的 2 个日期之间的选定客户订单或发票。

【问题讨论】:

    标签: php mysql sql pdo


    【解决方案1】:

    您在查询 :customerID:date 中有 2 个参数,但在 exeture() 中您传递的数组只有 1 个元素,您应该按以下方式修改查询代码

    $queryim = 'SELECT * FROM invoice WHERE customerID=:customerID and date BETWEEN :date1 AND :date2;
    $faturaSor=$db->prepare($queryim); 
    $faturaSor->execute(array('customerID'=>$_POST['customerID'],'date1'=>$_POST['firstDate'],'date2'=>$_POST['secondDate']));
    

    您还应该检查 $_POST 中的日期格式,它可以不同于数据库默认值 (YYYY-MM-DD HH:MM:SS)

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多