[英]Select multiple rows' ids and use them for the WHERE IN clause in an update query
已經嘗試了各種解決方案來解決我的問題,以從多個行中選擇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查詢,它應該為與請求ID和先前選擇的subject_ID相匹配的對應行更新表中的Teacher_id和status_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子句中的Array):
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
我很感謝每一個建議。
我相信您無需在此處指定表名
requests_subjects.subject_id IN
只需將其更改為
subject_id IN
並嘗試一下。
問題是,您需要將數據正確地正確放置在數組中...
$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_ASSOC
是PDOStatement::fetchAll
和PDOStatement::fetch
參數, 請檢查!
PDO :: FETCH_ASSOC:返回一個在結果集中返回的按列名索引的數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.