【问题标题】:Selection box insert multiple times in database选择框在数据库中多次插入
【发布时间】:2015-05-26 12:33:00
【问题描述】:

这是页面的样子:

这就是数据库的样子:

现在,我让这一切都与文本字段一起工作。 (当您按下保存时,文本字段会插入到 bogie_nr
现在,我不想有文本字段而是复选框。
所以,如果我选择前 2 个复选框,然后按保存。我希望数据库通过 axle_nr 1 和 2 插入数字 1
现在,我希望禁用前 2 个框(因此您不能再次检查它们)。

现在,当您选择第 3 个和第 4 个框时,然后按保存。我希望数据库在第三个和第四个axle_nr 之前插入数字2

填写完所有内容后,我想要一个将我重定向到新页面的按钮。 我该怎么做?

代码(仅用于复选框):

<tr>
    <?php   
        $show_axle = $database->bogies($_GET['train_id']);
        foreach($show_axle as $bogiebox){ ?>
            <input type='hidden' name='bogie_id[<?php echo $bogiebox['bogie_id']?>]' value='<?php echo $bogiebox['bogie_id']?>'>
            <td>
                <input type='checkbox' id="bogie_axle_fields" name='bogie_nr[<?php echo $bogiebox['bogie_id']?>]' placeholder = "enter bogie number">
            </td>
    <?php
        } 
    ?>
</tr>

功能:

function bogies($id){
        $sql = "SELECT * FROM bogie WHERE train_id = :id2";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":id2", $id, PDO::PARAM_STR);
        $sth->execute();
        return $sth->fetchAll();
    }

编辑:

在我的保存按钮后面是一个页面:end_result.php。就在那里我有一个功能:

function update_bogie($id) {
        $sql = "UPDATE bogie SET bogie_nr = :bogie_nr WHERE bogie_id = :bogie_id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(':bogie_id', $id, PDO::PARAM_INT);
        $sth->bindParam(":bogie_nr", $_POST['bogie_nr'][$id], PDO::PARAM_STR);
        $sth->execute();
    }

这会更新转向架表(当我将复选框更改为文本字段时有效)

我现在想要的: 复选框。当我选中前两个框时,然后按保存。我希望在数据库中为轴 1 和 2 插入 2 次数字 1。
之后,我希望禁用前 2 个框。所以你不能再次选择它们。

编辑:

ID 是什么样的:

几乎可以运行的编辑:

好的,现在复选框插入到数据库中。当它们被插入数据库时​​,它们也被禁用。现在只有几个问题:

  1. 当我第一次插入它们时。该值 = 1。但在 第二次插入它也是 1。而它应该是 2。
  2. 我需要刷新页面才能看到选择框 禁用。我希望这立即发生。
  3. 当我插入新值时。老(残疾人)回到NULL。但它应该保持旧值。

我现在拥有的代码:

<form method='POST'>
<input type="hidden" value="true" id="y" name="y">
    <div id="axle_bogie_border">
        <div id="train_adjusted">
            <h2>
                Train
            </h2>
        </div>
        <table id="distance_margin">
            <div id="bogiebox">
                <tr> 
                    <?php    
                        $x = 1;  
                        foreach($show_axle as $bogiebox){ ?>
                        <input type='hidden' name='bogie_id[<?php echo $bogiebox['bogie_id']?>]' value='<?php echo $bogiebox['bogie_id']?>'>
                        <td>
                            <?php 
                                if($bogiebox['bogie_nr'] == ''){
                            ?>
                                <input type='checkbox' id="bogie_axle_fields" value="<?= $x ?>" name='bogie_nr[<?php echo $bogiebox['bogie_id']?>]' placeholder = "enter bogie number"></td>
                            <?php
                                }
                                else{ 
                            ?>
                                <input type='checkbox' id="bogie_axle_fields" checked disabled value="<?= $x ?>" name='bogie_nr[<?php echo $bogiebox['bogie_id']?>]' placeholder = "enter bogie number"></td><?php } }
                    ?>
                </tr>
            </div>
        </table>
        <input type='submit' id="add_train_button1" value='Save'>

        <?php 
            if(isset($_POST['y'])){
            $validbogies = false;
                    //validate
                    if(($_POST['bogie_id']) >0){
                            if($_POST['bogie_id'] >0){
                                    $validbogies = true;
                            }elseif($_POST['bogie_id'] <= 0){
                                echo "Error!";
                            }
                            else{
                                    echo "Error!." . "<br>";
                            }
                    }else{
                            echo "Error!" . "<br>";
                    }

                    //If valid, then insert.
                    if($validbogies){
                        foreach($_POST['bogie_id'] as $id) {
                            $update_axle = $database->update_bogie($id);
                        }
                            $x++;
                            unset($_POST['y']);  
                            echo "Yea! Things have been moved to the database :)";

                    }
            }
        ?>
    </div>
</form>

而且功能还是和旧的一样。

编辑
这里举个例子:

【问题讨论】:

  • 这些复选框总是有 bogie_id 455-458 还是会有所不同?
  • 它们各不相同。当我添加一辆新的火车时,有 8 个车轴。他们将是 459-466
  • 您知道当前正在查看哪列火车吗?你已经在 php 或 javascript 中保存了你当前正在查看 117 号火车的地方?
  • 当我第一次插入我的火车时,我会询问最后插入的 train_id。它也在这个函数中:($_GET['train_id'])。
  • 这就是你所有的代码吗?因为你问的东西比你提供的代码要多,到目前为止你尝试了什么?

标签: php sql loops pdo mysqli


【解决方案1】:
<?php   
        $show_axle = $database->bogies($_GET['train_id']);

        // First question : When everything is filled in, 
        // i want a button that redirects me to a new page.

        if(count($show_axle) == 4)
           echo "<button type=\"button\" onClick=location.href=''>Click Me!</button>";

          // Second question :
         // WE display the missing checkboxes
        // We will display until we found the first value axle_nr
       // (i.e. the checkbox the user click last time)
      //  when we found we need to move to the next value of axle_nr
        else
        {
            $j = 0;
            for($i = 0; $i < 4; ++$i)
            {
                 if($show_axle[$j]['axle_nr'] != ($i + 1))
                 {
                    echo "<input type='hidden' name='bogie_id[" . 
                          $bogiebox['bogie_id']. "]' value='" .  
                          $bogiebox['bogie_id'] . "'><td>";

                    echo "<input type='checkbox' id='bogie_axle_fields' 
                            name='bogie_nr[" . $bogiebox['bogie_id'] . 
                            "]' placeholder='enter bogie number'></td>";

                 }
                 else
                   ++$j;
           }
?>

应该这样做(mb echo 有一些错误,但算法没问题)。

【讨论】:

  • 这对保存数据库中的值没有帮助,这只是为了显示按钮,但这里的问题是它也不会使复选框无法点击
【解决方案2】:

在保存按钮后面的代码中试试这个:(我假设你的复选框的 id 是这样的:bogie_nr[455]

    function update_bogie($id) {
            $sql = "UPDATE bogie SET bogie_nr = :bogie_nr WHERE bogie_id = :bogie_id";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':bogie_id', $id, PDO::PARAM_INT);
            if(isset($_POST['bogie_nr' . '[' . $id .']' ]){
              $sth->bindParam(":bogie_nr", $_POST['bogie_nr' . '[' . $id .']' ], PDO::PARAM_STR);
            }
            else {
              $sth->bindParam(":bogie_nr", 'NULL', PDO::PARAM_STR);
            }
            $sth->execute();
    }

修改正确的号码: Mitch 要获取您的号码,请尝试执行如下查询:

SELECT COUNT(*)
FROM bogie
WHERE train_id = @youridhere;

这将返回您已经拥有的那辆火车的转向架数量,只需根据您添加的元素数量执行 +1 或 +2(在 for 循环中使用计数器)并且您有正确的数字要插入

【讨论】:

  • 我认为这至少应该为您提供数据库中的某种数据
  • 致命错误:无法通过引用传递参数 2。在这一行: $sth->bindParam(":bogie_nr", NULL, PDO::PARAM_STR);
  • 首先,我们实际上并不想进入那个 else 语句,这意味着它没有找到您的复选框,您的复选框的名称是什么样的?
  • 我的意思是你的 html 元素在运行时是什么样子的:这个:name='bogie_nr[]
  • 这样,我刚刚编辑了什么?但后来才检查。 ]' placeholder = "输入转向架编号">
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2016-10-04
  • 2013-12-25
  • 2011-03-24
  • 2017-11-14
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多