简体   繁体   English

更新具有相同ID和相同值的多行

[英]Update multiple rows with same id and same value

I have a simple update query, which somehow doesn't work. 我有一个简单的更新查询,以某种方式不起作用。 Already trying to change it for some time and compared it to other update-queries but no matter what I change, it does not function. 已经尝试将其更改一段时间,并将其与其他更新查询进行比较,但是无论我更改了什么,它都无法正常工作。 Two rows in the same table with the same teacher_id should be updated with the same value. 同一表中具有相同Teacher_id的两行应更新为相同值。

Here is the query: 这是查询:

$StatusChangeQuery = "UPDATE teachers SET status_id = $status_id WHERE teacher_id = '20'";

$pdo->query($StatusChangeQuery);

I know it is a really simple one, but I keep getting the following error: 我知道这是一个非常简单的错误,但我一直收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 
1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for 
the right syntax to use near 'WHERE teacher_id = '20'' at line 1' in /var/www/xxx/html/teacher.php:225 
Stack trace: #0 /var/www/xxx/html/teacher.php(225): 
PDO->query('UPDATE teachers...') #1 {main} thrown in /var/www/xxx/html/teacher.php on line 225

And the corresponding teachers rows look like: 相应的教师行如下所示:

+---+-------------+------------+
|id | teacher_id  | status_id  |
+---+-------------+------------+
| 1 |     20      |      1     |
| 2 |     20      |      1     |
+---+-------------+------------+

As the comments suggest, there is probably an issue with $status_id . 如评论所建议, $status_id可能存在问题。

Since you are using PDO, it is advisable to prepare your statements. 由于您正在使用PDO,因此建议您准备语句。

$smt = $pdo->prepare("UPDATE teachers SET status_id=? WHERE teacher_id=?");
$smt->execute( array($status_id, 20) );

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

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