简体   繁体   English

PHP-PDO搜索查询,然后更新结果

[英]PHP-PDO Search query then update the result

Guys please see my code below. 伙计们,请看下面我的代码。 I really have no idea how to run my query. 我真的不知道如何运行我的查询。 what I want to achieve are I will first search in my database the DATA which is not yet finished or cancelled then the returned data will be updated the over due fields to NO. 我要实现的目标是,我将首先在数据库中搜索尚未完成或取消的DATA 然后将返回的数据更新为到期字段。 I run correctly the first query which is the searching. 我正确运行第一个查询,即搜索。 But I dont know how to make the second. 但是我不知道该怎么做。 please help. 请帮忙。 thanks. 谢谢。

<?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>

You can do this in a single query: 您可以在单个查询中执行此操作:

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

And btw your $TEST variable is not an array so you can't loop through it. 顺便说一句,您的$TEST变量不是数组,因此无法循环访问它。 It just contains the last value from the query. 它仅包含查询中的最后一个值。

Yes it will not give you any result because it just updates the database. 是的,它不会给您任何结果,因为它只是更新数据库。

But if you really want to display all the affected rows, your initial approach is ok but when it comes to the update query you're better off using WHERE IN instead of looping and doing a single update on each iteration since you already have all the ID's in your $TEST variable 但是,如果您确实想显示所有受影响的行,那么您的初始方法是可以的,但是当涉及更新查询时,最好使用WHERE IN而不是循环并在每次迭代中进行一次更新,因为您已经拥有了所有ID在您的$TEST变量中

$TEST contains id $ TEST包含ID

$TEST= $this->row->ID

But you expecting, what $TEST is array 但是您期望,什么是$ TEST是数组

foreach ($TEST as $value)

And i think, better way is to write you update query like this: 我认为,更好的方法是这样编写更新查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM