I have a problem with my PHP-Script. I want to insert data into my database but the only thing it returns is an Error.
Code:
<?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;
}
}
}
?>
In the ConnectionHandler it set up the Connection to the database.
Error:
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
答案 0 :(得分:0)
你不能以这种方式使用INSERT INTO
。在此上下文中不允许使用Subquerys。但标量表达式将成为诀窍:
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
最好的问候,binzram
PS: 下次乘车到我的办公桌。