简体   繁体   中英

Select multiple rows' ids and use them for the WHERE IN clause in an update query

Already tried various solutions to solve my problem to select the IDs (from multiple rows), which are then used to update specific rows that match these IDs.

I have currently deployed the following. First the simple SELECT query for the IDs, which are then used for the WHERE clause in the next UPDATE query:

$SelectTSubjectsQuery = "
SELECT subject_id FROM teachers_subjects
WHERE teacher_id = :teacher_id";

$statement = $pdo->prepare($SelectTSubjectsQuery);
$statement->bindParam(':teacher_id', $_SESSION['teacher_id']);
$statement->execute();

$FetchedTSubjects = Array();
$FetchedTSubjects = $statement->fetchAll();

Now the UPDATE query, which should update the teacher_id and status_id in the table for the corresponding rows that match the request id and the afore selected subject_IDs:

$StatusUpdateQuery = "UPDATE requests_subjects SET teacher_id = :teacher_id, status_id = '2' WHERE request_id = :request_id AND requests_subjects.subject_id IN (".implode(',', $FetchedTSubjects).")";

$statement = $pdo->prepare($StatusUpdateQuery);
$statement->bindParam(':request_id', $_GET['id']);
$statement->bindParam(':teacher_id', $_SESSION['teacher_id']);
$statement->execute();

My requests_subjects table, which should be updated, looks like:

+---+-------------+------------+----------------+------------+
|id | request_id  | subject_id |   teacher_id   |  status_id |
+---+-------------+------------+----------------+------------+
| 1 |     19      |      1     |       0        |      1     |
| 2 |     19      |      2     |       0        |      1     |
| 2 |     19      |      3     |       0        |      1     |
| 2 |     20      |      1     |       4        |      3     |
| 2 |     21      |      1     |       5        |      3     |
+---+-------------+------------+----------------+------------+

So, eg if the teacher with the teacher_id = '2' with the subject_ids '2' and '3' opts for the request with the request_id = '19' then the rows with request_id = '19' and subject_ids '2' and '3' should be updated with the teacher_id '2' and the status_id = '2'.

edit: oh and of course the error I currently get (obviously it doesn't work with the Array in current WHERE clause):

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'' in /var/www/xxx/html/teacher.php:227 Stack trace: #0 /var/www/xxx/html/teacher.php(227): PDOStatement->execute() #1 {main} thrown in /var/www/xxx/html/teacher.php on line 227

I am thankful for every advice.

I believe you don't need to specify the table name here

requests_subjects.subject_id IN 

Just change it to

subject_id IN 

and give it a try.

Well the problem, is that you need to put the data right in the array correctly...

$SelectTSubjectsQuery = "SELECT subject_id FROM teachers_subjects WHERE teacher_id = :teacher_id";

$statement = $pdo->prepare($SelectTSubjectsQuery);
$statement->bindParam(':teacher_id', $_SESSION['teacher_id']);
$statement->execute();

$FetchedTSubjects = Array();
$FetchedTSubjects = $statement->fetchAll()

From your code if you print_r($FetchedTSubjects) will get the following result or similar:

Array
(
    [0] => Array
        (
            [subject_id] => 1
            [0] => 1
        )

    [1] => Array
        (
            [subject_id] => 2
            [0] => 2
        )
)

if you try to implode(',',$FetchedTSubjects) you actually are trying to convert an array to string so that is the meaning of

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause''

You need to do the following:

$SelectTSubjectsQuery = "SELECT subject_id FROM teachers_subjects WHERE teacher_id = :teacher_id";

$statement = $pdo->prepare($SelectTSubjectsQuery);
$statement->bindParam(':teacher_id', $_SESSION['teacher_id']);
$statement->execute();

$FetchedTSubjects = Array();

// iterate all the rows to get the subject_id of each and put in an independent array
foreach($statement->fetchAll(\PDO::FETCH_ASSOC) as $subject) {
    array_push($FetchedTSubjects,$subject['subject_id']);
}

now if you print_r($FetchedTSubjects) you will get the following output:

Array
(
    [0] => 1
    [1] => 2
)

You now you can successfully use implode(',', $FetchedTSubjects) that will return (1,2) as you expect it to do so

NOTE: \\PDO::FETCH_ASSOC is parameter for PDOStatement::fetchAll and PDOStatement::fetch check it out!

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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