将基于复选框的数组插入mysql数据库

时间:2018-02-10 18:33:34

标签: php mysql

正在完成一项学校作业,以制作用于将书籍输入数据库的网络表格。我被困在一个需要将图书类别插入数据库book_categories表的部分。该表仅包含isbn和categoryid。将书籍插入书籍表格没有问题,我认为这与将数组插入数据库有关,而isbn保持不变(用户可以选择多个类别)。

我将首先发布我认为最相关的部分,然后我将粘贴我的整个代码。我会说我知道sql查询是正确的,因为我可以将变量分解为实数值,它将插入到数据库中。

foreach ($book_categories as $categoryid) {

            $sql3=file_get_contents('setCategory.sql');

            $params2 = array(
            'isbn' => $isbn,
            'category' => $categoryid);

            $statement2=$database->prepare($sql3);
            $statement2->execute($params2);
            }

完整代码

 <?php

    // Create and include a configuration file with the database connection
    include('config.php');
    // Get an associative array of categories from the database
    $sql=file_get_contents('category.sql');


    $statement=$database->prepare($sql);
    $statement->execute();

    $categories = $statement->fetchAll(PDO::FETCH_ASSOC);



    // Get type of form either add or edit from the URL (ex. form.php?action=add)
    $action=$_GET['action'];

    // If form submitted    
        // if the type of form specified in the URL is add
            // Get variables from the form submitted using $_POST
            // Insert the book into the database
            // Set the categories of the book in the database

            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $isbn = $_POST['isbn'];
                $title = $_POST['book-title'];
                $book_categories = $_POST['book-category'];
                $author = $_POST['book-author'];
                $price = $_POST['book-price'];

                $sql2=file_get_contents('insertBook.sql');

                $params = array(
                'isbn' => $isbn,
                'author' => $author,
                'title' => $title,
                'price' => $price
            );

                $statement2=$database->prepare($sql2);
                $statement2->execute($params);



                foreach ($book_categories as $categoryid) {

                $sql3=file_get_contents('setCategory.sql');

                $params2 = array(
                'isbn' => $isbn,
                'category' => $categoryid);

                $statement2=$database->prepare($sql3);
                $statement2->execute($params2);
                }

                header('location: index.php');
                die();
            }


        // Redirect to book listing page


    ?>

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">

        <title>Add New Book</title>
        <meta name="description" content="The HTML5 Herald">
        <meta name="author" content="SitePoint">

        <link rel="stylesheet" href="css/style.css">

        <!--[if lt IE 9]>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="page">
            <h1>Add New Book</h1>
            <form action="" method="POST">
                <div class="form-element">
                    <label>ISBN:</label>
                    <input type="text" name="isbn" class="textbox" />
                </div>
                <div class="form-element">
                    <label>Title:</label>
                    <input type="text" name="book-title" class="textbox" />
                </div>
                <div class="form-element">
                    <label>Category:</label>
                    <?php 
                    foreach($categories as $category) : ?>
                    <input class="radio" type="checkbox" name="book-category[]" value=<?php $category['categoryid'] ?> /><span class="radio-label"><?php echo $category['name'] ?></span><br />
                    <?php endforeach ?>
                </div>
                <div class="form-element">
                    <label>Author</label>
                    <input type="text" name="book-author" class="textbox" />
                </div>
                <div class="form-element">
                    <label>Price:</label>
                    <input type="number" step="any" name="book-price" class="textbox" />
                </div>
                <div class="form-element">
                    <input type="submit" class="button" />&nbsp;
                    <input type="reset" class="button" />
                </div>
            </form>
        </div>
    </body>
    </html>

0 个答案:

没有答案