切换为编辑来自 PHP 的 Mysql 数据

Togle to edit Mysql data from PHP

我的 php 中有一个 table,其中显示来自 mysql 的 'users' table 的数据。在 table 中,我添加了一个编辑按钮,这样,如果您单击该按钮,就会在 table 下方显示一个表单,您可以在其中编辑 table 内容的某些字段,我还在它旁边添加了一个删除按钮来删除你想要的 table 行,但那个按钮工作正常。

我遇到的问题是编辑按钮,当我按下该按钮时,它不会显示我在 table 下面添加的表单,但它会将我带到页面 'registerTeacher.php?edit = 3' 例如。按 'Edit' 按钮,它应该显示可以编辑的表单,但它什么也没做,也没有显示任何错误。有人可以帮我找到问题出在哪里吗?谢谢。

这是我的“indexAdmin.php”代码:

<?php
include('server.php');
include('Security.php');
include('Conexion.php');
include('registerTeacher.php');
?>

<?php
$style = "style='display:none;'";
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $update = true;

    $record = mysqli_query($conn, "SELECT * FROM users WHERE id=$id");

    if (@count($record) == 1 ) {
        $n = mysqli_fetch_array($record);
        $username = $n['username']; 
        $email  = $n['email']; 
        $style = "style='display:block;'";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="styleIndexAdmin.css">
    <meta charset="utf-8">
</head>
<body>

<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
    <div class="error success" >
        <h3>
            <?php
            echo $_SESSION['success'];
            unset($_SESSION['success']);
            ?>
        </h3>
    </div>
<?php endif ?>

<!-- logged in user information -->
<?php  if (isset($_SESSION['username'])) : ?>


    <div class="navbar" id="navbar">
        <a class="tablink" onclick="openPage('professorsRegistered', this, 'lightblue')" id="defaultOpen">Profesores</a>
    </div>

    <!----------- PROFESSROS REGISTERED TABLE ----------->

    <div id="professorsRegistered" class="tabcontent"></br><br>

        <table class="professorsRegistered">
            <tr>
                <th colspan="3"><h2>PROFESSORS REGISTERED</h2></th>
            </tr>
            <tr>
                <th> Name </th>
                <th> Edit </th>
                <th> Delete </th>
            </tr>

        <?php
        $result = $conn->query($sql);
        $sql = "SELECT * FROM users WHERE rol='profesor'"; 
        $result = $conn->query($sql);
        if ($result->num_rows==0){
            echo 'No professors';
        }else{
            while($row = $result->fetch_assoc()) {
                echo "<tr>
                          <td>".$row["username"]."</td>
                          <td><a href='registerTeacher.php?edit=".$row["id"]."' class='edit_btn' ><i class='fa fa-pencil-square-o' style='cursor:pointer;'></i></a></td>

                          <td><a class='eliminate' onClick=\"javascript: return confirm('Confirm to delete');\" href=\"deleteTeacher.php?id=".$row['id']."\">X</a></td>
                          </tr>";
            }
        }
        ?>
        </table><br><br>

        <!------- FORM TO EDIT REGISTERED TEACHERS ---------->
        <form method="post" action="registerTeacher.php" <?php echo $style;?>>
            <!----- newly added field--->
            <input type="hidden" name="id" value="<?php echo $id; ?>">

            <div class="input-group">
                <label>USER</label>
                <input type="text" name="username" value="<?php echo $username; ?>">
            </div>
            <div class="input-group">
                <label>EMAIL</label>
                <input type="email" name="email" value="<?php echo $email; ?>">
            </div>
            <div class="input-group">
                <!--------BUTTON----->
                <?php if ($update == true): ?>
                    <button class="btn_update" type="submit" name="update">EDIT</button>
                <?php endif ?>
            </div>
        </form>
    </div>

<?php endif ?>

<script>
    function getUrlVars(){
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value){
            vars[key] =value;
        });
        return vars;
    }

    function openPage(pageName,elmnt,color) {
        var i, tabcontent, tablinks;
        tabcontent = document.getElementsByClassName("tabcontent");
        for (i = 0; i < tabcontent.length; i++) {
            tabcontent[i].style.display = "none";
        }
        tablinks = document.getElementsByClassName("tablink");
        for (i = 0; i < tablinks.length; i++) {
            tablinks[i].style.backgroundColor = "";
        }
        document.getElementById(pageName).style.display = "block";
        elmnt.style.backgroundColor = color;
    }
    p = getUrlVars()["page"];
    //alert(p);
    if (p==undefined){
        document.getElementById("defaultOpen").click();
    }else{
        openPage(p, this, 'lightblue');
    }

</script>

</body>

<?php if (isset($_SESSION['message'])): ?>
    <div class="msg">
        <?php
        echo $_SESSION['message'];
        unset($_SESSION['message']);
        ?>
    </div>
<?php endif ?>
</html>

这是我的“registerTeacher.php”文件:

<?php
include('Conexion.php');

$username = "";
$email = "";
$errors = array();
$id = 0;
$update = false;

// UPDATE
if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $username = $_POST['username']; 
    $email = $_POST['email'];

    mysqli_query($conn, "UPDATE users SET username='$username', email='$email' WHERE id=$id");
    $_SESSION['message'] = "User edited!";
    header("Location: indexAdmin.php?page=professors");
}
?>

因为 <a href=..> 用于在您的情况下重定向到某个页面,它会重定向到您在 href attribute.Instead 中指定的 registerTeacher.php 页面,您可以喜欢以下:

单击 <a href..> 下方时,您可以将表格放在 registerTeacher.php 上,如下所示:

<?php 

    if (isset($_GET['edit'])) {
        $id = $_GET['edit'];
        $update = true;

        $record = mysqli_query($conn, "SELECT * FROM users WHERE id=$id");

        if (@count($record) == 1 ) {
            $n = mysqli_fetch_array($record);
            $username = $n['username']; 
            $email  = $n['email']; 
            $style = "style='display:block;'";
        }
    }
?>

     <!------- FORM TO EDIT REGISTERED TEACHERS ---------->
        <form method="post" action="abcpage.php" <?php echo $style;?>>
            <!----- newly added field--->
            <input type="hidden" name="id" value="<?php echo $id; ?>">

            <div class="input-group">
                <label>USER</label>
                <input type="text" name="username" value="<?php echo $username; ?>">
            </div>
            <div class="input-group">
                <label>EMAIL</label>
                <input type="email" name="email" value="<?php echo $email; ?>">
            </div>
            <div class="input-group">
                <!--------BUTTON----->
                <?php if ($update == true): ?>
                    <button class="btn_update" type="submit" name="update">EDIT</button>
                <?php endif ?>
            </div>
        </form>

然后您可以将此详细信息传递给您的 action="abcpage.php"。并在该页面中像下面这样写,即:

// UPDATE
if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $username = $_POST['username']; 
    $email = $_POST['email'];

    mysqli_query($conn, "UPDATE users SET username='$username', email='$email' WHERE id=$id");
    $_SESSION['message'] = "User edited!";
    header("Location: indexAdmin.php?page=professors");
}
?>

希望对您有所帮助!

注意 :也尝试使用 prepared statement 它是安全的。