Bagaimana mengatasi gagal mengganti username admin dan password ?

bagaimana mengatasi gagal mengganti username dan password untuk admin, kode ini berhasil di user biasa namun tidak berhasil digunakan di profil admin. berikut saya lampirkan kodenya.

<?php

include('header.php');

$admin_user_name = '';
$admin_password = '';
$error_admin_user_name = '';
$error_admin_password = '';
$error = 0;
$success = '';

if (isset($_POST["button_action"])) {
    if (empty($_POST["admin_user_name"])) {
        $error_admin_user_name = 'Nama Admin harus di isi';
        $error++;
    } else {
        $admin_user_name = $_POST["admin_user_name"];
    }

    if (!empty($_POST["admin_password"])) {
        $admin_password = $_POST["admin_password"];
    }


    if ($error == 0) {
        if ($admin_password != "") {
            $data = array(
                ':admin_user_name'           =>  $admin_user_name,
                ':admin_password'       =>  password_hash($admin_password, PASSWORD_DEFAULT),
                ':admin_id'             =>  $_POST["admin_id"]
            );

            $query = "
      UPDATE tbl_admin
      SET admin_user_name = :admin_user_name,
      admin_password = :admin_password,
      WHERE admin_id = :admin_id
      ";
        } else {

            $data = array(
                ':admin_user_name'           =>  $admin_user_name,
                ':admin_id'             =>  $_POST["admin_id"]
            );

            $query = "
      UPDATE tbl_admin
      SET admin_user_name = :admin_user_name,
      WHERE admin_id = :admin_id
      ";
        }

        $statement = $connect->prepare($query);
        if ($statement->execute($data)) {
            $success = '<div class="alert alert-success">Profil anda sudah diperbaharui</div>';
        }
    }
}

$query = "
SELECT * FROM tbl_admin
WHERE admin_id = '" . $_SESSION["admin_id"] . "'
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();

?>

<div class="container" style="margin-top:30px">
    <span id="message_operation"><?php echo $success; ?></span>
    <div class="card">
        <form method="post" id="profile_form">
            <div class="card-header">
                <div class="row">
                    <div class="col-md-9">Profil</div>
                    <div class="col-md-3" align="right">

                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="form-group">
                    <div class="row">
                        <label class="col-md-4 text-right">Nama Admin<span class="text-danger">*</span></label>
                        <div class="col-md-8">
                            <input type="text" name="admin_user_name" id="admin_user_name" class="form-control" />
                            <span class="text-danger"><?php echo $error_admin_user_name; ?></span>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="row">
                        <label class="col-md-4 text-right">Password <span class="text-danger">*</span></label>
                        <div class="col-md-8">
                            <input type="password" name="admin_password" id="admin_password" class="form-control" placeholder="Kosongkan Jika Tidak Ingin Diubah" />
                            <span class="text-danger"></span>
                        </div>
                    </div>
                </div>

            </div>

            <div class="card-footer" align="center">

                <input type="hidden" name="admin_id" id="admin_id" />
                <input type="submit" name="button_action" id="button_action" class="btn btn-success btn-sm" value="Simpan" />

            </div>
        </form>
    </div>
</div>

</body>
</html>

<script>
    $(document).ready(function() {

        <?php
        foreach ($result as $row) {
            ?>
            $('#admin_user_name').val("<?php echo $row['admin_user_name']; ?>");
            $('#admin_id').val("<?php echo $row['admin_id']; ?>");
        <?php
        }
        ?>


    });
</script>

avatar dhanis
@dhanis

9 Kontribusi 1 Poin

Dipost 5 tahun yang lalu

Tanggapan

errornya apa?

tidak bisa update data nama admin dan ganti password.

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban