PDO $ statement->可以执行多个参数吗?

时间:2018-10-17 07:05:54

标签: php sql pdo

这是我无法理解的最后一个部分,我希望有人可以对丢失的内容有所了解。

这段代码的较长部分可以正常工作,它是UPDATE部分(在  dis_adjust.php文件)引起了悲伤。

代替硬编码13,它需要接受变量'catID'。

变量“ catID”最初会过滤为数字1,然后在单击“奖励”按钮时更新为5。

不知道我是在正确解释还是在清楚……也许是睡眠不足。

dis_adjust.php

<?php
require 'dis_db.php';
$tag_h_id = $_GET['id'];
$sql = 'UPDATE `coh_items` SET `enabled`=5, `rewarded_at`= now()  WHERE `category_id`=13 AND `tag_h_id`=:id';
$statement = $connection->prepare($sql);
if ($statement->execute([':id' => $tag_h_id])) {
header("Location: http://localhost/green/management/disbursements.php");
}

<?php
   require 'dis_db.php';
   
   $sql = 'SELECT
       coh_items.id,
       coh_items.tag_h_id,
       coh_vendors.name,
       coh_vendors.phone,
       coh_vendors.tax_number,
       coh_vendors.currency_code,
       coh_items.purchase_price,
       COUNT(tag_h_id) AS Reports,
       (
           purchase_price * COUNT(tag_h_id)
       ) AS Reward
   FROM
       coh_items
   INNER JOIN coh_vendors ON coh_items.tag_h_id = coh_vendors.id
   WHERE
       coh_items.enabled = 1 AND category_id = ?
   GROUP BY
       tag_h_id;';
   	
   $statement = $connection->prepare($sql);
   //$statement  ->bindParam(':category_id', $catID, PDO::PARAM_INT);
    $statement->execute([$_POST['catID']]);
   $people = $statement->fetchAll(PDO::FETCH_OBJ);
   
    ?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Disbursement</title>
      <link rel="stylesheet" href="http://localhost/green/vendor/almasaeed2010/adminlte/bootstrap/css/bootstrap.min.css">
      <!--    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script> 
      <!-- <script src="http://localhost/green/vendor/almasaeed2010/adminlte/bootstrap/js/bootstrap.js"></script> -->
      <style type="text/css">
      </style>
      <script type="text/javascript">
         $(document).ready(function(){
             $('[data-toggle="tooltip"]').tooltip();   
         });
      </script>
   </head>
   <body>
      <div class="wrapper">
      <div class="container-fluid">
      <div class="row">
         <div class="col-md-12">
            <div class="page-header clearfix">
               <h3 class="center">Details by Category</h3>
               <div>
                  <!--Filter Button  -->
                  <div>
                     <div class="col-md-2">
                        <p class="margin"></p>
                        <div class="input-group">
                           <div class="input-group-btn">
                              <form action="http://localhost/green/management/disbursements.php" method="post">
                              <input type="text" name = "catID" placeholder="Category" class="form-control" >
                              <input type="submit" value= "Filter" class="btn btn-primary ">Filter</button></d>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
               <br>
               <br>
               <div class="col-md-8">
                  <table class="table table-bordered">
                     <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Zip</th>
                        <th>Status</th>
                        <th>Number of Reports</th>
                        <th>Award</th>
                        <th>Action</th>
                     </tr>
                     <?php foreach($people as $person): ?>
                     <tr>
                        <td><?= $person->tag_h_id; ?></td>
                        <td><?= $person->name; ?></td>
                        <td><?= $person->phone; ?></td>
                        <td><?= $person->tax_number; ?></td>
                        <td><?= $person->currency_code; ?></td>
                        <td><?= $person->Reports; ?></td>
                        <td><?= $person->Reward; ?></td>
                        <td> 
                           <a  href="./disbursements/dis_adjust.php?id=<?= $person->tag_h_id ?>" name = 'adjust' class='btn btn-success'>Rewarded</a>
                        </td>
                     </tr>
                     <?php endforeach; ?>
                  </table>
               </div>
            </div>
         </div>
      </div>

1 个答案:

答案 0 :(得分:2)

PDO prepared statements可以接受任意数量的参数。要将您的查询修改为使用变量$catId,请将查询更改为:

$sql = 'UPDATE `coh_items` SET `enabled`=5, `rewarded_at`= now()  WHERE `category_id`=:cat_id AND `tag_h_id`=:id';

execute呼叫:

$statement->execute([':id' => $tag_h_id, ':cat_id' => $catID])