我希望我的按钮更改状态设置,但不是更改它,而是转到href页面

时间:2018-03-18 09:18:04

标签: php html mysql pdo

首先:据我所知,目前还没有YouTube视频或教程。我找到了一些,但它们是JavaScript,但我不知道它,所以我不能在很短的时间内完成。

我最近发布了同样的问题但是根据一些人的建议改变了我的代码。我遇到了很多错误而不是解决方案......

无论如何,我有一张我的网站图片和我想做的事情。

网站: https://imgur.com/a/hcpuA

这是我想要做的照片: https://imgur.com/a/Vuuk9

最后我的代码在下面,我删除了非常必要的HTML部分。

adduser.php:

    <?php
    session_start();
        if (!isset($_SESSION['username']))
        {
            header('location: login.php');
        die();
        }


    ?>


    <!DOCTYPE html>
    <html>
    <head>
        <title>Manage users</title>
        <link rel="icon" type="image/png" sizes="16x16" href="image/favicon-16x16.png">
        <link rel="icon" type="image/png" sizes="32x32" href="image/favicon-32x32.png">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link rel="stylesheet" type="text/css" href="css/table.css">
    </head>
    <body>
        <?php include ("header.php"); ?>

    <?php 

    require ("config.php");

    ?>


    <form name="frmSearch" method="post" action="adduser.php">
    <div class="table-container">
        <div class="table-something">
            <div class="table-header">
                <span id="message"></span>
                <h2>Admin List<span class="blink">_</span> </h2>
                <input name="var1" type="text" id="var1" />
                <input class="dede" type="submit" name="search" value="search" />
            </div>
        <div class="table-body">
            <table class="table-hen">

    <?php 
    if (isset($_POST['var1'])) {
    $var1 = $_POST['var1'];
    }
    else {
        $var1 = 1;
    }

    $sql= "SELECT user_id, fname, mname, lname, username, type, a_e_num, user_status FROM users WHERE a_e_num LIKE :search";

    $stmt = $db->prepare($sql);
    $stmt->bindValue(':search', '%' . $var1 . '%', PDO::PARAM_INT);
    $stmt->execute();

    if ($stmt->rowCount() > 0) {
        ?>
        <tr>
                    <th>User Id</th>
                    <th>Name </th>
                    <th>Username</th>
                    <th>Employee # </th>
                    <th>User Type</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
                <?php
    $result = $stmt->fetchAll();

    foreach ($result as $row): 

        ?>


                <tr>
                    <td><?php echo $row['user_id']; ?></td>
                    <td><?php echo $row['fname']; ?>  <?php echo $row['mname']; ?> <?php echo $row['lname']; ?> </td>
                    <td><?php echo $row['username']; ?></td>
                    <td><?php echo $row['a_e_num']; ?> </td>
                    <td><?php echo $row['type']; ?></td>

                    <td>
                        <?php if ($row['user_status']=='Enable') echo "Active"; 
                        if ($row['user_status']=='Disable') echo "Disabled" ?>
                        </td>
                    <td>
                    <?php
                    $user_id = $row['user_id'];
                    $status = '';
                    if ($row['user_status'] == 'Enable') {
                        $status = 'Enable';
                    }
                    else if ($row['user_status'] == 'Disable') {
                        $status = 'Disable';
                    } 
                    ?>
                    <?php
                    $user_id = $row['user_id'];
                    $status = $row['user_status'];
                    ?>
                        <a class="archive" action="archive.php" onclick="
                        return confirm('Are you sure you want to <?php if ($row['user_status']=='Enable') echo "disable"; 
                        if ($row['user_status']=='Disable') echo "enable"?> this user account?')" 
                        href="archive.php?user_id=<?= $user_id?>&status=<?=$status?>">

                        <?php if ($row['user_status']=='Disable')   
                        echo "Unarchive"; 
                        if ($row['user_status']=='Enable') 
                            echo "Archive" ?>

                            </a></td>
                </tr>

    <?php endforeach; 
    } else {
    echo 'there is nothing to show';
    }
    ?>
    </table>
    </form>
    </div>
    </div>
    </div>

    <br><h1></h1>
    <br><h1></h1>
    <br><h1></h1>
    <br><h1></h1>
    <br><h1></h1>

        <?php include ("footer.php"); ?>


    </body>
    </html>

这是我的Archive.php:

    <?php
    require ("config.php");

    $user_id= $_GET['user_id'];
    $user_status = $_GET['user_id'];

    $query = $db->prepare ("SELECT * FROM users WHERE user_id = :user_id, user_status = :user_status");

    $query->bindParam(':user_id', $user_id);
    $query->bindParam(':user_status',$user_status);
    $query->execute();

    if ($user_status=='Enable')
    {
        $sql = "UPDATE users SET user_status = 'Disable' WHERE user_id = :user_id";
    }
    if ($user_status=='Disable')
    {
        $sql = "UPDATE users SET user_status='Enable' WHERE user_id = :user_id";
    }

    if ($query->execute([':user_status'=>$user_status, ':user_id'=>$user_id])){

        header("Location:adduser.php");
    }
    ?>

1 个答案:

答案 0 :(得分:1)

首先,您需要清除语句并将参数分开,如下所示。

第1步:

参数是(查询字符串),以?开头,更多参数后面跟&

<?php
$user_id = $row['user_id'];
$status = '';
if ($row['user_status'] == 'Enable') {
    $status = 'Enable';
} else if ($row['user_status'] == 'Disable') {
    $status = 'Disable';
}
?>
<a href="archive.php?user_id=<?=$user_id?>&user_status=<?=$status?>">

如果您的$row['user_status']值始终与条件语句相同,则您不需要条件只需按照赋值变量。

<?php
$user_id = $row['user_id'];
$status = $row['user_status'];
?>
<a href="archive.php?user_id=<?=$user_id?>&status=<?=$status?>">

第2步: 那么这个

$id= $_GET['user_id'];

$user_id= $_GET['user_id'];
$user_status = $_GET['user_status'];

因为您的绑定参数采用未在Archive.php

中定义的变量
$query->bindParam(':user_id', $user_id);
$query->bindParam(':user_status',$user_status);

编辑2:

改变你的`从

开始的地方
<td>
<?php
$user_id = $row['user_id'];
$status = $row['user_status'];

只需替换为以下内容。

<td>
    <?php
    $user_id = $row['user_id'];
    $status = $row['user_status'];
    $btn_confirm = '';
    if ($status == 'Enable') {
        $btn_confirm = "disable";
    } else if ($status == 'Disable') {
        $btn_confirm = "enable";
    }
    ?>
    <a class="archive" onclick="return confirm('Are you sure you want to <?= $btn_confirm; ?> this user account?')"
       href="Archive.php?user_id=<?= $user_id ?>&status=<?= $status ?>">
        <?php if ($status == 'Disable') {
            echo "Unarchive";
        } else if ($status == 'Enable') {
            echo "Archive"; }
        ?>

    </a>
</td>
  

注意:您的文件名为Archive.php而非archive.php这会导致空白页面出现问题