【问题标题】:Button deleting all users in SQL Table按钮删除 SQL 表中的所有用户
【发布时间】:2017-12-01 09:07:32
【问题描述】:

我有一个网站,我从一个程序中提取所有当前的活动用户,并且在管理面板中,我需要一个“终止开关”。

它从一个“在线”的 SQL 表中拉出用户,然后 php 将它变成一个数组。

<?php
    $stack = array();
    for($id = 1; $id <= 20; $id++)
    {
        $db = Database::getInstance();

        $stmt = $db->prepare('SELECT * FROM online WHERE id = :id LIMIT 1');
        $stmt->execute([':id' => $id]);
        $user = $stmt->fetchObject('User');
        array_push($stack, $user);
    }
?>

这会遍历表并获取所有 ID 为 [1;20] 的在线用户。 然后它将 $user 堆叠在数组 $stack 中。

<table>
    <thead><tr><th colspan="4"><span>Online right now</span></th></tr><tr><th colspan="4"> </th></tr><tr><th><span>E-Mail</span></th><th><span>Telefone</span></th><th><span>Unit ID</span></th></tr></thead>
    <tbody>
        <?php foreach ($stack as $num) : ?>
            <?php if($num != null) : ?>
                <tr>
                    <th><?=htmlspecialchars($num->email) ?></th>
                    <th><?=htmlspecialchars($num->telefone) ?></th>
                    <th><?=htmlspecialchars($num->unitid) ?></th>
                    <th><form action="thisPage.php" method="post"><input type="submit" name="killClick" value="Kill"/></form></th>
                </tr>
            <?php endif ?>
        <?php endforeach ?>
    </tbody>
</table>

这只是通过 $stack 将每个 $user 或 $num 分配给表中的一行,该行包含用户的电子邮件、电话号码、unitID 和 a 按钮。

<?php
    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['killClick']))
    {
        $passedId = $num->id;

        try
        {
            $db = Database::getInstance();

            $stmt = $db->prepare('DELETE FROM online WHERE id = :id LIMIT 1');
            $stmt->execute([':id' => $passedId]);
        }
        catch (PDOException $exception)
        {
            error_log($exception->getMessage());
            return false;
        }
    }
?>

上面的 PHP 位于同一页面上,并且在 foreach 循环之外。

所以目前当我按下按钮时,它会删除“在线”表中的所有用户......为什么?以及如何让它只删除我点击的用户?

【问题讨论】:

  • 循环中的表单没有将任何 ID 值传递给删除函数 ~ 也许您应该在其中有一个隐藏字段,其中包含该特定条目的 ID,以便在表单被传递时传递发布
  • 哦,是的,没想到!会试试的!

标签: php mysql button foreach


【解决方案1】:

要发送每个特定记录的 ID,请在循环中生成的表单中包含一个隐藏字段,然后在脚本的删除部分中使用 $_POST['id'] 作为 $passedID 值。

<?php
    if( $_SERVER['REQUEST_METHOD']=="POST" and isset( $_POST['killClick'],$_POST['id'] ) ) {

        $passedId = $_POST['id'];

        try {
            $db = Database::getInstance();

            $stmt = $db->prepare('DELETE FROM online WHERE id = :id LIMIT 1');
            $stmt->execute( [ ':id' => $passedId ] );
        } catch( PDOException $exception ) {
            error_log( $exception->getMessage() );
            return false;
        }
    }
?>




<table>
    <thead>
        <tr>
            <th colspan='4'><span>Online right now</span></th>
        </tr>
        <tr>
            <th colspan='4'></th>
        </tr>
        <tr>
            <th><span>E-Mail</span></th>
            <th><span>Telefone</span></th>
            <th><span>Unit ID</span></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($stack as $num) : ?>
            <?php if($num != null) : ?>
                <tr>
                    <th><?=htmlspecialchars($num->email) ?></th>
                    <th><?=htmlspecialchars($num->telefone) ?></th>
                    <th><?=htmlspecialchars($num->unitid) ?></th>
                    <th>
                        <form action='thisPage.php' method='post'>
                            <input type='hidden' name='id' value='<?=$num->id;?>' />
                            <input type='submit' name='killClick' value='Kill'/>
                        </form>
                    </th>
                </tr>
            <?php endif ?>
        <?php endforeach ?>
    </tbody>
</table>

您可以使用 between 运算符而不是使用循环来简化第一个 sql 查询。

即:

$num=array();
$db = Database::getInstance();

$sql='select * from `online` where `id` between :id_low and :id_high;';
$stmt=$db->prepare( $sql );

if( $stmt ){
    $stmt->execute( array( ':id_low'=>1, ':id_high'=>20 ) );
    $num=$stmt->fetchAll( PDO::FETCH_OBJ );
}

【讨论】:

    【解决方案2】:

    您在提交表单时忘记了 $_POST['num']。

    顺便说一句,您可以使用 GET 获取此案例 id 吗?>">killClick

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2015-03-10
      相关资源
      最近更新 更多