【问题标题】:PHP-PDO Search query then update the resultPHP-PDO 搜索查询然后更新结果
【发布时间】:2015-04-22 05:01:48
【问题描述】:

伙计们,请在下面查看我的代码。我真的不知道如何运行我的查询。我想要实现的是我将首先在我的数据库中搜索尚未完成或取消的数据,然后返回的数据将被更新为“否”。 我正确运行了第一个查询,即搜索。但我不知道如何制作第二个。请帮忙。谢谢。

<?php

require 'connection.php';

class updateOverdue{
    private $db,$sql,$stmt,$row;

    public function __construct(){
        $this->db = new connection();
        $this->db = $this->db->dbConnect();

    }
    public function updatefields(){
        $this->sql = "SELECT * FROM jrf_tbl WHERE status <> 'Finished' AND status <> 'Cancelled'";
        $this->stmt = $this->db->prepare($this->sql);
        $this->stmt->execute();

            if($this->stmt->rowCount()){
                while($this->row = $this->stmt->fetch(PDO::FETCH_OBJ)){
                 echo "
                        <tbody>
                            <tr> 
                                <td>",$TEST= $this->row->ID, "</td> 
                                <td>",$this->row->strjrfnum, "</td>
                                <td>",$this->row->strstatus,"</td>
                                <td>",$this->row->strpriority,"</td>
                                <td>",$this->row->strdate,"</td>
                                <td>",$this->row->strduedate,"</td>
                            </tr>
                        </tbody>";
                  }
            };

            foreach ($TEST as $value) { /* here is my question */
                $this->sql = "UPDATE jrf_tbl SET strifoverdue ='no' WHERE ID=". $value;
                $this->stmt = $this->db->prepare($this->sql);
                $this->stmt->execute();
            }
        }

}//End Class

?>

    <table border=1>
        <thead>
            <tr>
                <th>ID</th>
                <th>JRF Num</th>
                <th>Status</th>
                <th>Priority</th>
                <th>Date received</th>
                <th>Due Date</th>
            </tr>
        </thead>
        <?php 
            $OverDue = new updateOverdue();
            $OverDue->updatefields();
        ?>
    </table>

【问题讨论】:

    标签: php pdo


    【解决方案1】:

    您可以在单个查询中执行此操作:

    UPDATE jrf_tbl SET strifoverdue ='no'
        WHERE status <> 'Finished' AND status <> 'Cancelled'"
    

    顺便说一句,您的 $TEST 变量不是数组,因此您无法循环遍历它。它只包含查询中的最后一个值。

    是的,它不会给你任何结果,因为它只是更新数据库。

    但是,如果您真的想显示所有受影响的行,那么您的初始方法是可以的,但是当涉及到更新查询时,您最好使用 WHERE IN 而不是循环并在每次迭代中进行一次更新,因为您$TEST 变量中的所有 ID 都已包含

    【讨论】:

    • 天啊!!我想了很多我想念最简单的事情。谢谢你!我的坏我是新手=_=。啊,你知道吗,或者你有什么参考。关于,我想更新所有过期的数据。?
    • 它就像 UPDATE jrf_tbl SET strifoverdue ='no' WHERE status 'Finished' AND status 'Cancelled' AND overdue_date > date_now" 这有效吗?因为我没有结果
    • 我明白了,非常感谢!!
    【解决方案2】:

    $TEST 包含 id

    $TEST= $this->row->ID
    

    但你期待,$TEST 是数组

    foreach ($TEST as $value)
    

    我认为,更好的方法是像这样编写更新查询:

    UPDATE jrf_tbl SET strifoverdue ='no' 
    WHERE status <> 'Finished' AND status <> 'Cancelled'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 2010-11-25
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      相关资源
      最近更新 更多