子查询returns多于一行?不能写入数据库
Subquery returns more than one row? Can't write in database
我的 PHP-Script 有问题。我想将数据插入我的数据库,但唯一 returns 是一个错误。
代码:
<?php
require_once('ConnectionHandler.php');
Class CreateTask
{
public function createNewTask($description, $subject, $type, $enddate, $priority)
{
$query = 'INSERT INTO task (description, subject_id, task_type_id, enddate, priority) VALUES (?, (SELECT id_subject FROM subject WHERE name = ?), (SELECT id_task_type FROM task_type WHERE type = ?), ?, ?)';
$statement = ConnectionHandler::getConnection()->prepare($query);
$statement->bind_param('siisi', $description, $subject, $type, $enddate, $priority);
if (!$statement->execute()) {
throw new Exception($statement->error);
} else {
return true;
}
}
}
?>
在 ConnectionHandler 中它设置了到数据库的连接。
错误:
Fatal error: Uncaught exception 'Exception' with message 'Subquery returns more than 1 row' in E:\xampp\htdocs\lib\CreateTask.php:16 Stack trace: #0 E:\xampp\htdocs\homelogedin.php(21): CreateTask->createNewTask('fgdgdsg', 'Mathematics', 'Exam', '2015-05-31', '3') #1 E:\xampp\htdocs\pages\index.php(4): require_once('E:\xampp\htdocs...') #2 E:\xampp\htdocs\lib\CentralDesign.php(35): require_once('E:\xampp\htdocs...') #3 E:\xampp\htdocs\index.php(9): CentralDesign->loadPage() #4 {main} thrown in E:\xampp\htdocs\lib\CreateTask.php on line 16
你不能那样使用 INSERT INTO
。在此上下文中不允许使用子查询。但是标量表达式将达到目的:
INSERT INTO task (description, subject_id, task_type_id, enddate, priority)
SELECT 'desc', A.id_subject, B.id_task_type, '2015-05-05', 2
FROM
(SELECT id_subject FROM subject WHERE name = 'Blau') A,
(SELECT id_task_type FROM task_type WHERE type = 'typ1') B
此致,宾兹拉姆
PS:
下次到我的办公桌去吧。
我的 PHP-Script 有问题。我想将数据插入我的数据库,但唯一 returns 是一个错误。
代码:
<?php
require_once('ConnectionHandler.php');
Class CreateTask
{
public function createNewTask($description, $subject, $type, $enddate, $priority)
{
$query = 'INSERT INTO task (description, subject_id, task_type_id, enddate, priority) VALUES (?, (SELECT id_subject FROM subject WHERE name = ?), (SELECT id_task_type FROM task_type WHERE type = ?), ?, ?)';
$statement = ConnectionHandler::getConnection()->prepare($query);
$statement->bind_param('siisi', $description, $subject, $type, $enddate, $priority);
if (!$statement->execute()) {
throw new Exception($statement->error);
} else {
return true;
}
}
}
?>
在 ConnectionHandler 中它设置了到数据库的连接。
错误:
Fatal error: Uncaught exception 'Exception' with message 'Subquery returns more than 1 row' in E:\xampp\htdocs\lib\CreateTask.php:16 Stack trace: #0 E:\xampp\htdocs\homelogedin.php(21): CreateTask->createNewTask('fgdgdsg', 'Mathematics', 'Exam', '2015-05-31', '3') #1 E:\xampp\htdocs\pages\index.php(4): require_once('E:\xampp\htdocs...') #2 E:\xampp\htdocs\lib\CentralDesign.php(35): require_once('E:\xampp\htdocs...') #3 E:\xampp\htdocs\index.php(9): CentralDesign->loadPage() #4 {main} thrown in E:\xampp\htdocs\lib\CreateTask.php on line 16
你不能那样使用 INSERT INTO
。在此上下文中不允许使用子查询。但是标量表达式将达到目的:
INSERT INTO task (description, subject_id, task_type_id, enddate, priority)
SELECT 'desc', A.id_subject, B.id_task_type, '2015-05-05', 2
FROM
(SELECT id_subject FROM subject WHERE name = 'Blau') A,
(SELECT id_task_type FROM task_type WHERE type = 'typ1') B
此致,宾兹拉姆
PS: 下次到我的办公桌去吧。