public function insertBook($bookName, $bookAuthor) { $stmt = mysqli_prepare($this->connection, 'INSERT INTO books(book_title) VALUES (?)'); mysqli_stmt_bind_param($stmt, 's', $bookName); mysqli_stmt_execute($stmt); $authorId = []; $author = new Author(); $allAuthors = $author->selectAllAuthors(); foreach ($bookAuthor as $au) { foreach ($allAuthors as $key => $value) { if ($au == $value) { $authorId[] = $key; } } } $books = $this->getBook(); $keyID = 0; foreach ($books as $key => $title) { if ($title == $bookName) { $keyID = $key; } } $stmt2 = mysqli_prepare($this->connection, 'INSERT INTO books_authors(book_id,author_id) VALUES (?,?)'); for ($i = 0; $i < count($authorId); $i++) { mysqli_stmt_bind_param($stmt2, 'ii', $keyID, $authorId[$i]); mysqli_stmt_execute($stmt2); } }
<?php /** * Created by PhpStorm. * User: gdimitrov * Date: 30.9.2015 г. * Time: 12:54 ч. */ include '../DB/mySQL.php'; include '../Model/Author.php'; include '../Model/BookModel.php'; $aut = new Author(); $authors = $aut->selectAllAuthors(); if ($_POST) { $bookAuthors = $_POST['authors']; $bookName = $_POST['bookName']; $book = new BookModel(); $book->insertBook($bookName, $bookAuthors); header('Location: ../View/allBooks.php'); }
<?php /** * Created by PhpStorm. * User: gdimitrov * Date: 30.9.2015 г. * Time: 10:55 ч. */ include '../DB/mySQL.php'; include '../Model/Author.php'; try { $aut = new Author(); if ($_POST) { $author = trim($_POST['authorName']); if (mb_strlen($author) < 3) { $minLenght = 'Името на автора не трябва да е по-малко от 3 символа.'; } else { if ($aut->checkRepeatName($author)) { $minLenght = 'Автора: ' . $author . ' , който се опитвате да добавите, вече съществува в списъка!'; } else { $aut->insertAuthor($author); header('Location: ../View/allBooks.php'); } } } $booksAndAuthors = $aut->selectAllAuthors(); } catch (Exception $exc) { echo $exc->getMessage(); }