对于在php中选择的多个复选框,数据不会存储在数据库中

时间:2015-07-20 11:30:49

标签: php mysql database

下面显示的是我的表格,它显示产品和圈子作为复选框,我需要做的是存储在数据库中选中的复选框值。

1 #myform文件名= checkbox1.php

                            <!DOCTYPE html>
                            <!--
                            To change this license header, choose License Headers in Project Properties.
                            To change this template file, choose Tools | Templates
                            and open the template in the editor.
                            -->




                            <html>
                                <head>
                                    <meta charset="UTF-8">
                                    <title></title>
                                </head>
                                <body>
                                    <form name="pdt_circle" method="GET" action="store.php">
                                        <?php
                                        $conn = mysqli_connect("localhost", "root", "", "demo");
                                        if (mysqli_errno($conn)) {
                                            echo 'database error' . mysqli_errno($conn);
                                        } else {
                                            $query = "select * from demo1";
                                            $result = mysqli_query($conn, $query);
                                            if ($result) {
                                                echo '<table>';
                                                echo '<tr><th>Select</th><th>Product</th><th>Circle</th></tr>';
                                                while ($row = mysqli_fetch_assoc($result)) {
                                                    echo '<tr><td>';
                                                    echo "<input type=\"checkbox\" name=\"product[]\" id=\"product[]\"  value=\"" . $row['Product'] . "\" ></td><td> " . $row['Product'];
                                                    echo '</td>';
                                                    echo '<td>';
                                                    echo "<input type=\"checkbox\" name=\"circle[]\" id=\"circle[]\"  value=\"" . $row['Circle'] . "\" ></td><td>" . $row['Circle'];
                                                    echo '</td>';
                                                    echo '</tr>';
                                                }
                                                echo '</table>';
                                            }
                                        }
                                        ?>
                                        <input type="submit" value="submit" name="sub" ><br>                  

                                    </form>

                                </body>
                            </html>

我还创建了名为store.php的数据库文件。当我点击提交时,没有显示错误。但是数值不会存储在数据库中。

2#store.php(数据库文件)

                                    <?php

                                    $conn = mysqli_connect("localhost", "root", "", "demo");
                                    if (mysqli_errno($conn)) {
                                        echo 'Database error';
                                    } else {
                                        echo 'i n else';

                                        if (isset($_GET['circle']) and isset($_GET['product'])) {
                                            if (is_array($_GET['circle']) and is_array('product')) {
                                                $count = count($_GET['checkbox']);


                                                foreach ($_GET['product'] as $pdt) { {
                                                        echo '';
                                                        $query1 = "insert into demo2 (Product) values ('$pdt')";
                                                        mysqli_query($conn, $query1);
                                                    }
                                                    foreach ($_GET['circle'] as $cir) { {
                                                            echo $cir;
                                                            $query = "insert into demo3 (Circle) values ('$cir')";
                                                            mysqli_query($conn, $query);
                                                        }
                                                        mysqli_close($conn);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    ?>

3#sqlfiles写如下

--
                -- Database: `demo`
                --

                -- --------------------------------------------------------

                --
                -- Table structure for table `demo1`
                --

                CREATE TABLE IF NOT EXISTS `demo1` (
                  `id` int(5) NOT NULL AUTO_INCREMENT,
                  `Product` varchar(25) NOT NULL,
                  `Circle` varchar(25) NOT NULL,
                  PRIMARY KEY (`id`),
                  UNIQUE KEY `id` (`id`)
                ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

                --
                -- Dumping data for table `demo1`
                --

                INSERT INTO `demo1` (`id`, `Product`, `Circle`) VALUES
                (3, 'hello', 'surat'),
                (4, 'callertune', 'ahmd'),
                (5, 'hello', 'guj'),
                (6, 'callertune', 'mp'),
                (7, '3g', 'rajasthan'),
                (8, 'data', 'mumbai'),
                (9, 'callme', 'kolkata'),
                (10, 'airtel_123', 'JK');

                /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
                /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
                /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

SQL2:表名= DEMO2

--
                            -- Database: `demo`
                            --

                            -- --------------------------------------------------------

                            --
                            -- Table structure for table `demo2`
                            --

                            CREATE TABLE IF NOT EXISTS `demo2` (
                              `id` int(5) NOT NULL AUTO_INCREMENT,
                              `Product` varchar(25) NOT NULL,
                              PRIMARY KEY (`id`),
                              UNIQUE KEY `id` (`id`)
                            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

                            /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
                            /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
                            /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

3#表名:demo3

                                        --
                                        -- Database: `demo`
                                        --

                                        -- --------------------------------------------------------

                                        --
                                        -- Table structure for table `demo3`
                                        --

                                        CREATE TABLE IF NOT EXISTS `demo3` (
                                          `id` int(5) NOT NULL AUTO_INCREMENT,
                                          `Circle` varchar(25) NOT NULL,
                                          PRIMARY KEY (`id`)
                                        ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

                                        /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
                                        /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
                                        /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

表单形式的链接: https://drive.google.com/file/d/0B1rpCWtUcfePM3hVZEpHLXByeW8/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

一些指示:

checkbox1.php &lt; - 包含多个id =&#34; circle []&#34;这是无效的(但不会导致您的问题)。

另外,为什么不使用POST而不是GET?

<强> store.php 调试时,请始终显示已发布的数据,以便检查您正在处理的内容,如下所示:

echo "<pre>";
print_r($_GET);
echo "<pre>";
exit;

删除此项检查:

if (is_array($_GET['circle']) and is_array('product')) {

你知道它何时被设置,那必须是一个数组。

另外,我认为你并不想检查产品是否&#39;是一个数组。我希望您想查看$ _GET [&#39;产品&#39;]。

请看下面的代码,稍微清理一下:

$conn = mysqli_connect("localhost", "root", "", "demo");
if (mysqli_errno($conn)) {
        echo 'Database error';
} else {
    $postedCircles = (isset($_GET['circle']))?$_GET['circle']:array();
    $postedProducts = (isset($_GET['product']))?$_GET['product']:array();
    foreach ($postedCircles as $cir) {
        // You are totally vuerable to SQL injection here, don't do it like this
        $query = "insert into demo3 (Circle) values ('".$cir."')";
        echo $query;
        mysqli_query($conn, $query);
    }

    foreach ($postedProducts as $pdt) {
        // You are totally vuerable to SQL injection here, don't do it like this
        $query = "insert into demo2 (Product) values ('".$pdt."')";
        echo $query;
        mysqli_query($conn, $query);
    }

    mysqli_close($conn);
}

它产生什么输出? 那是法律问题吗?你能用手对mysql运行吗?

最后,请不要在查询中使用来自客户端的原始值。 以这种方式插入不需要的SQL非常容易(查找:SQL注入)