单击按钮时SQL更新表行

时间:2019-02-22 07:25:41

标签: php html sql

单击按钮后,我试图更新表中的特定行列。在我的表格中,所有行都有一个“预定”列,其唯一键是“ orderid”

enter image description here

现在在我的显示表中(每个都使用a),我在按钮中添加了if条件(如果为1,则为1,否则为0)。

<td>
 <?php
    require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

    if (($rowOrder['sched']) == 1) {
    ?>
    button type="submit" id="sched_button" name="sched_button" value="SCHED_BUTTON" class="btn btn-success">YES</button>
    <?php
    } else if (($rowOrder['sched']) == 0) {
    ?>
    <button type="submit" id="sched_button" name="sched_button" value="SCHED_BUTTON" class="btn btn-danger">NO</button>
 <?php
    }
    ?> 
</td>

enter image description here

这是我的按钮代码。我试图将0更改为1或将1更改为0。我知道我的SQL Update缺少某些内容。我可以添加什么以便更新计划的列? (取决于我点击按钮的特定行)

我把“计划的”列变成了“计划的”快捷方式

  if (isset($_POST['sched_button']) && $_POST['sched_button'] == 'SCHED_BUTTON')
  {
    if (($rowOrder['sched']) == 1) {
      $stmt = $db->prepare('UPDATE order SET scheduled = 0'));
      $stmt->execute();
    } else if (($rowOrder['sched']) == 0) {
      $stmt = $db->prepare('UPDATE order SET scheduled = 1'));
      $stmt->execute();
    }

  }

5 个答案:

答案 0 :(得分:1)

这是更新scheduled列的快速方法:

显示器

<td>
<?php
    require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

    if (($rowOrder['sched']) == 1) {
    ?>
    <button type="submit" id="sched_button" name="sched_button" value="1" class="btn btn-success">YES</button>
    <?php
    } else if (($rowOrder['sched']) == 0) {
    ?>
    <button type="submit" id="sched_button" name="sched_button" value="0" class="btn btn-danger">NO</button>
<?php
    }
    ?> 
</td>

查询

<?php

if (!empty($_POST['sched_button']))
{
    $stmt = $db->prepare("UPDATE order SET scheduled = ?"));
    $stmt->bind_param("i", $_POST['sched_button'];
    $stmt->execute();
}

答案 1 :(得分:0)

将按钮values设置为1或0,然后在提交后将其完成

编辑;正如@Hasta Dhana所说,您缺少列packageid

答案 2 :(得分:0)

     <button type="submit" id="sched_button" name="sched" value="<?php echo ($rowOrder['sched'] == 1 ? 0 : 1); ?>" class="btn btn-<?php echo ($rowOrder['sched'] == 1 ? "success" : "danger"); ?>"><?php echo ($rowOrder['sched'] == 1 ? "YES" : "NO"); ?></button>

if (isset($_POST['sched'])) {
                $stmt = $db->prepare("UPDATE order SET packageid =$_POST['sched']");
                $stmt->execute();
            }

答案 3 :(得分:0)

display.php

<td>
    <?php
    require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

    if (($rowOrder['sched']) == 1) {
        ?>
        <button  class="btn btn-success" onclick="update(1,<?php echo $rowID; ?>)">YES</button>
        <?php
    } else if (($rowOrder['sched']) == 0) {
        ?>
        <button class="btn btn-danger" onclick="update(0,<?php echo $rowID; ?>)">NO</button>
        <?php
    }
    ?>
</td>

<script>
    function update(value,id) {

        $.ajax({
            url: "update.php",
            type: "POST",
            data: {"id": id, "value":value},
            success: function () {
                console.log('Updated');
            },
            error: function (err) {
                console.log(err);
            }
        });

    }
</script>

update.php

<?php
require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

if(isset($_POST['id'])&& isset($_POST['value'])){

    $id = $_POST['id'];
    $value = $_POST['value'];

    $stmt = $db->prepare("UPDATE order SET scheduled = $value WHERE orderid = ?");
    $stmt->bind_param('i', $id);  // Bind "$id" to parameter. 
    $stmt->execute();

}

答案 4 :(得分:0)

因此,您的问题是,表单处理程序不知道要处理哪个orderid。您可以将表单调整为类似以下内容:

<td>
    <?php
    require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();
    ?>
    <button type="submit" id="sched_button" name="sched_button_<?= $rowOrder['sched'] ?>" value="<?= $rowOrder['orderid'] ?>" class="btn btn-success">YES</button>
</td>

这将以sched_button_0sched_button_1提交,其值为订单ID。

您的处理程序可能看起来像这样:

if (isset($_POST['sched_button_0'])) {
    $stmt = $db->prepare('UPDATE order SET scheduled = 1 WHERE orderid = ?');
    $stmt->bind_param('i', $_POST['sched_button_0']);
    $stmt->execute();
} else if (isset($_POST['sched_button_1'])) {
    $stmt = $db->prepare('UPDATE order SET scheduled = 0 WHERE orderid = ?');
    $stmt->bind_param('i', $_POST['sched_button_0']);
    $stmt->execute();
}

我假设您正在使用mysqli连接数据库。