【问题标题】:I want my button to change the Status set, but instead of changing it, goes to a href page instead我希望我的按钮更改状态集,但不是更改它,而是转到一个 href 页面
【发布时间】:2018-08-27 00:38:28
【问题描述】:

首先:据我所知,没有 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");
    }
    ?>

【问题讨论】:

  • 你的问题不够清楚,需要在不刷新网页的情况下更新用户状态?那么您将必须使用 ajax/javascript
  • 请完成您的html代码并回答@hassan问题...
  • 我只想在用户单击按钮后将状态更改为禁用或启用,而不使用Js或Ajax。
  • 我更新了完整的html代码,并做了一个更清晰的标题。谢谢!

标签: php html mysql pdo


【解决方案1】:

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

第 1 步:

参数为(查询字符串),以?开头,更多参数后跟&amp;

<?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 这会导致空白页出现问题

【讨论】:

  • 我替换了你说的所有内容,但它没有将我的 user_status 更新为禁用或启用,相反,它仍然进入名为 localhost/sample2/archive.php?user_id=2&status=Enable 的空白页面我是忘记了什么?
  • 对不起,我的错误应该是$user_status = $_GET['user_status'];
  • 我得到了一个错误,叫做 Undefined index: user_status 在我放置它的同一行。
  • 比较编辑代码和你的代码。我现在改变了锚标签&lt;a href="archive.php?user_id=&lt;?=$user_id?&gt;&amp;user_status=&lt;?=$status?&gt;"&gt;
  • 已编辑,感谢您的快速回复!不过还是和第一个问题一样,进入空白页,不编辑状态表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 2019-08-30
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
相关资源
最近更新 更多