【问题标题】:Why do my PHP Delete button and functions only work firefox?为什么我的 PHP 删除按钮和功能只适用于 firefox?
【发布时间】:2020-05-08 18:11:06
【问题描述】:

你好 Stack Overflowers!

我的 my_admin.php 文件有问题,我的删除按钮只在 firefox 上处理,没有其他浏览器,这是一个棘手的问题,我认为这可能是我更新到新版本的 php 但放回原来的版本和相同的问题:(如果有人可以快速查看我的核心代码,看看他们是否能发现错误。my_admin 的代码如下。下面是 pdocon.php 和我的 functions.php 都包含在my_admin.php 文件。谢谢大家!

<?php include('includes/header.php'); ?>


<?php

//Include functions
include('includes/functions.php');

?>

<?php
/************** Fetching data from database using id ******************/

//require database class files
require('includes/pdocon.php');

//instatiating our database objects
$db = new Pdocon;

//Create a query to select all users to display in the table

$db->query("SELECT * FROM admin WHERE email=:email");

$email  =   $_SESSION['user_data']['email'];

$db->bindValue(':email', $email, PDO::PARAM_STR);

//Fetch all data and keep in a result set
$row = $db->fetchSingle();

?>

<div class="row">
    <div class="col-md-12">
        <h2 class="text-center">My Account</h2>


    </div>
</div>

<div class="container">

    <div class="row">


        <div class="col-md-9">


            <?php showmsg(); ?>

            <?php if ($row) { ?>


                <form class="form-horizontal" role="form" method="post" action="">
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="name" style="color:#f3f3f3;">Fullname:</label>
                        <div class="col-sm-10">
                            <input type="name" name="name" class="form-control" id="name" value="<?php echo $row['fullname'] ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="email" style="color:#f3f3f3;">Email:</label>
                        <div class="col-sm-10">
                            <input type="email" name="email" class="form-control" id="email" value="<?php echo $row['email'] ?>" required>
                        </div>
                    </div>
                    <div class="form-group ">
                        <label class="control-label col-sm-2" for="pwd" style="color:#f3f3f3;">Password:</label>
                        <div class="col-sm-10">
                            <fieldset disabled>
                                <input type="password" name="password" autocomplete="yes" class="form-control disabled" id="pwd" value="<?php echo $row['password'] ?>" required>
                            </fieldset>
                        </div>
                    </div>

                    <br>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <a class="btn btn-primary" href="edit_admin.php?admin_id=<?php echo $row['id'] ?>">Edit</a>
                            <button type="submit" class="btn btn-danger pull-right" name="delete_form">Delete</button>
                        </div>
                    </div>

                </form>
        </div>

        <div class="col-md-3">
            <a href="edit_admin.php?admin_id=<?php echo $row['id'] ?>">

                <?php $image = $row['image']; ?>

                <?php echo ' <img src="uploaded_image/' . $image . '"  style="padding-top:25px; padding-bottom:30px;width:220px;-webkit-border-radius:50%;border-radius:50%;">'; ?>
            </a>
        </div>

    <?php } ?>






    <?php

    /************** Deleting data from database when delete button is clicked ******************/

    if (isset($_POST['delete_form'])) {

        $admin_id = $_SESSION['user_data']['id'];

        keepmsg('<div class="alert alert-danger text-center">

              <strong>Confirm!</strong> Do you want to delete your account? <br>
              <a href="#" class="btn btn-default" data-dismiss="alert" aria-label="close">No, Thanks</a><br>
              <form action="my_admin.php" method="post" action="my_admin.php">
              <input type="hidden" value="' . $admin_id . '" name="id"><br>
              <input type="submit" name="delete" value="Yes, Delete" class="btn btn-danger">
              </form>
            </div>');
    }



    //If the Yes Delete (confim delete) button is click from the closable div proceed to delete


    if (isset($_POST['delete'])) {

        $id = $_POST['id'];

        $db->query('DELETE FROM admin WHERE id=:id');

        $db->bindValue(':id', $id, PDO::PARAM_INT);

        $run = $db->execute();

        if ($run) {
            redirect('logout.php');
        } else {

            keepmsg('<div class="alert alert-danger text-center">
                      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                      <strong>Sorry </strong>User with ID ' . $id . ' Could not be deleted 
                </div>');
        }
    }

    ?>

    </div>

</div>
<?php include('includes/footer.php'); ?>

pdocon.php

<?php
class Pdocon
{


    // The connection Properties


    //Localhost Db information
    private $host       = "localhost";
    private $user       = "root";
    private $pass       = "";
    private $dbnm       = "wm_app";

    //Online Db information
    private $host       = "localhost";
    private $user       = "whiteman_dbadmin";
    private $pass       = "dingleberries";
    private $dbnm       = "whiteman_wmdb"; 


    private $dbh;


    private $errmsg;

    //Statement Handler
    private $stmt;


    //Method to open our connection

    public function __construct()
    {

        $dsn = "mysql:host=" . $this->host . "; dbname=" . $this->dbnm;

        $options = array(

            PDO::ATTR_PERSISTENT    => true,

            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION

        );

        try {

            $this->dbh  = new PDO($dsn, $this->user, $this->pass, $options);

            //echo "Successfully Connected";

        } catch (PDOException $error) {

            $this->errmsg = $error->getMessage();

            echo $this->errmsg;
        }
    }



    //Write query helper function using the stmt property
    public function query($query)
    {

        $this->stmt = $this->dbh->prepare($query);
    }


    //Creating a bind function 
    public function bindvalue($param, $value, $type)
    {

        $this->stmt->bindValue($param, $value, $type);
    }


    //Function to execute statement
    public function execute()
    {

        return $this->stmt->execute();
    }


    //Function to check if statement was successfully executed
    public function confirm_result()
    {

        $this->dbh->lastInsertId();
    }

    //Command to fetch data in a result set in associative array
    public function fetchMultiple()
    {

        $this->execute();

        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    //Command count fetched data in a result set 

    public function fetchSingle()
    {

        $this->execute();

        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }
}

functions.php

<?php

//function to trim values
function cleandata($value)
{

    return trim($value);
}



//function to sanitize value for string
function sanitize($raw_value)
{

    return filter_var($raw_value, FILTER_SANITIZE_STRING);
}



//function to validate value for email
function valemail($raw_email)
{

    return filter_var($raw_email, FILTER_VALIDATE_EMAIL);
}

//function to validate value for integer
function valint($raw_int)
{

    return filter_var($raw_int, FILTER_VALIDATE_INT);
}


//function to redirect
function redirect($page)
{

    header("Location: {$page}");
}


//function to keep error and success messages in a session 
function keepmsg($message)
{

    if (empty($message)) {

        $message = "";
    } else {

        $_SESSION['msg']    =   $message;
    }
}


//function to display the stored message in the session super global
function showmsg()
{

    if (isset($_SESSION['msg'])) {

        echo $_SESSION['msg'];

        unset($_SESSION['msg']);
    }
}


//Create function to hash password using md5
function hashpassword($clean_password)
{

    return md5($clean_password);
}

【问题讨论】:

  • 所以,在其他浏览器中,您单击按钮。那么会发生什么?显示警报或什么都不显示
  • 根本没有改变,页面做了一些快速的部分刷新然后什么也没有,在 Firefox 中你点击删除一次它什么都不做,然后你第二次点击确认警报出现并点击是,删除是处理。其他浏览器、chrome edge、edge-chronium 都什么都不做?
  • 感谢 Johannes,不幸的是它并没有改进功能,但是我在连续单击 20 次后才发现它最终会出现?这么奇怪?我可以确认该功能确实删除了管理员,但只有在大约第 20 次点击之后?
  • 我尝试了链接中大多数认可的选项,例如 action="?" action="#" 或完全删除它。但仍需要点击 20 次?

标签: php html mysql forms


【解决方案1】:

通过将 if(isset) 函数重新排列到初始代码上方并且删除函数起作用来解决问题,还将提交按钮类型设置为提交。修复了自己的问题!

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2022-01-07
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多