选择多行' id并将它们用于更新查询中的WHERE IN子句

时间:2016-01-06 14:42:28

标签: php mysql pdo

已经尝试了各种解决方案来解决我的问题,以选择ID(来自多行),然后用于更新与这些ID匹配的特定行。

我目前部署了以下内容。首先是ID的简单SELECT查询,然后在下一个UPDATE查询中用于WHERE子句:

$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();

现在是UPDATE查询,它应该更新表中的teacher_id和status_id,以查找与请求ID和前面选择的subject_ID匹配的相应行:

$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();

我的requests_subjects表应该更新,如下所示:

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

所以,例如如果教师用teacher_id =' 2'使用subject_ids' 2'和' 3'使用request_id =' 19'选择请求然后是request_id =' 19'和subject_ids' 2'和' 3'应该使用teacher_id' 2'进行更新。和status_id =' 2'。

编辑:哦,当然我目前得到的错误(显然它不能在当前的WHERE子句中使用数组):

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

我很感谢你的每一条建议。

2 个答案:

答案 0 :(得分:0)

我相信你不需要在这里指定表名

requests_subjects.subject_id IN 

只需将其更改为

即可
subject_id IN 

并尝试一下。

答案 1 :(得分:0)

问题是,您需要正确地将数据放入数组......

$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()

如果您print_r($FetchedTSubjects)获得以下结果或类似结果,请从您的代码中获取:

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

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

如果您尝试implode(',',$FetchedTSubjects)实际上是在尝试将数组转换为字符串,那么这就是

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

您需要执行以下操作:

$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']);
}

现在,如果您print_r($FetchedTSubjects),您将获得以下输出:

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

您现在可以成功使用implode(',', $FetchedTSubjects),它会按预期返回(1,2)

注意:\PDO::FETCH_ASSOCPDOStatement::fetchAllPDOStatement::fetch check it out!

的参数
  

PDO :: FETCH_ASSOC:返回按结果集中返回的列名索引的数组