当选中复选框时,PHP MySQL将数据插入表中各个

时间:2016-07-15 15:00:21

标签: php mysql checkbox pdo

我正在尝试根据选中的复选框将值插入到不同的数据库表中。我希望能够在此选定的表中插入1或0,具体取决于按下的按钮。有了这个,就需要有一个计数器来计算最多点击一次按钮的次数。假如用户选择5个复选框我想运行插入1或0,10次然后移动到下一个复选框/表。我正在使用Prepared Statementments和PDO。 代码的想法如下:

For every checkbox
Insert SQL Query for checkbox variable
When button pressed insert into correct db table
If either button is pressed 10 times between them
Move onto next checkbox value

插入页面:

function addScoreToDB() {
              try {
                $q = $connect->prepare('INSERT INTO :stand VALUES (:hit)');
                $q->bindParam(':stand', $_POST['stand']);
                $q->bindParam(':hit', $_POST['addhit']);
                $q->bindParam(':miss', $_POST['miss']);
                $q->execute();
              } catch (PDOException $e) {
                die("Could not connect to the database $dbname :" . $e->getMessage());
              }

            }


            foreach($_POST['stand'] as $stand):
              echo $stand;

            if(isset($_POST['hit']) || isset($_POST['miss'])):

                addScoreToDB();
              endif;
            endforeach;
    <form action="<?php echo $PHP_SELF;?>" method="post">
                <input type="hidden" value="1" name="addhit" />
                <input type="submit" accesskey="x" value="1" name="hit" class="button" />
                <input type="hidden" value="0" name="addmiss" />
                <input type="submit" accesskey="o" value="0"  name="miss" class="button"/>
            </form>

插入页面上的表格:

<form id="settings" action="add.php" method="post">
            Shooter Name: <?php
                echo "<select name='shooters'>";
              while($row = $q->fetch(PDO::FETCH_ASSOC)):
                echo "<option value=\"". $row['username'] ."\">" . $row['username'] . "</option>";
              endwhile;
                echo "</select>";
            ?>
            <br/>
            Stand One:<input type="checkbox" name="stand[]" value="stand_one" />
            Stand Two:<input type="checkbox" name="stand[]" value="stand_two" />
            Stand Three:<input type="checkbox" name="stand[]" value="stand_three" />
            </form>

0 个答案:

没有答案