An error har occured.
', $e->getMessage(), '
'; die(); } $sth = $dbh->query("SELECT id FROM Category WHERE name = '$category'"); $sth->setFetchMode(PDO::FETCH_ASSOC); $result = $sth->fetchAll(); if (count($result) > 0) { foreach ($result as $r) { echo "Category Id: " . $r['id'] . "\n"; $categoryId = $r['id']; } } else { // Insert new category?? echo "Category not found $category" . PHP_EOL; die(); } $csv = fopen($file,'r'); while (!feof($csv)) { $csvArray[] = fgetcsv($csv,1000,','); } fclose($csv); foreach ($csvArray as $value) { $data = ['question' => $value[1], 'answer' => $value[0]]; $sth = $dbh->prepare('INSERT INTO Questions (question, answer) VALUES (:question, :answer)'); try { $sth->execute($data); $questionId = $dbh->lastInsertId(); $sth2 = $dbh->prepare('INSERT INTO QuestionToCategory (questionId, categoryId) VALUES (:questionId, :categoryId)'); $questionLinkData = ['questionId' => $questionId, 'categoryId' => $categoryId]; $sth2->execute($questionLinkData); } catch (Exception $e) { echo '

An error has ocurred.

', $e->getMessage(), '
'; } } ?>