刷新googlechrome会导致pdo重复插入数据

yhqotfr8  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(217)

这是我的密码

class DBHelper{
private static $_instance;
private $conn;
private static $options = [
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "UTF8"',
    //PDO::ATTR_AUTOCOMMIT => false,
];
private function __construct($dsnm,$user,$password){
    try {
        $this->conn = new PDO($dsnm,$user,$password,self::$options);
    } catch (PDOException $e) {
        echo  'Connection failed: '  .  $e -> getMessage ();
        exit();
    }
    return $this->conn;
}

public static function get_db_instance($dsnm,$user,$password){
    if((self::$_instance instanceof self) == false){
        self::$_instance = new self($dsnm,$user,$password);
    }
    return self::$_instance;
}

public function query($sql,$array){
    if(is_array($array)){
        //$sth = self::$_instance->conn->prepare($sql);
        $sth = self::$_instance->conn->prepare($sql);
        return $sth->execute($array);  //return bool
        //var_dump(debug_backtrace());
    }
}

public function row($sql,$array){
    $sth = self::$_instance->conn->prepare($sql);
    $sth->execute($array);
    return $sth->fetch(PDO::FETCH_OBJ);
}

public function row_array($sql,$array){
    $sth = self::$_instance->conn->prepare($sql);
    $sth->execute($array);
    return $sth->fetch(PDO::FETCH_ASSOC);
}

public function rows($sql,$array = ''){
    if(is_array($array)){
        $sth = self::$_instance->conn->prepare($sql);
        $sth->execute($array);
        return $sth->fetchAll(PDO::FETCH_OBJ);
    }else{
        $sth = self::$_instance->conn->query($sql);
        return $sth->fetchAll(PDO::FETCH_OBJ);
    }
}

public function rows_array($sql,$array = ''){
    if(is_array($array)){
        $sth = self::$_instance->conn->prepare($sql);
        $sth->execute($array);
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }else{
        $sth = self::$_instance->conn->query($sql);
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
}

$db = DBHelper::get_db_instance("mysql:host=localhost;dbname=blog5.4;charset=UTF8", "root", "");
var_dump($db->query('insert into wechat (name,siteid,phone) values(?,?,?)',['lelelele','0000',time()]));

然后我刷新google浏览器,mysql数据库有两个相同的行图像。
一开始我认为这是我脚本中的一个bug,所以我用这个连接修改了它:

$conn = new PDO("mysql:host=localhost;dbname=blog5.4;charset=UTF8", "root", "");

var_dump($conn->exec("insert into wechat (name,siteid,phone) values ('lelelele','0000','15996573139')")); //return true

不幸的是,这也插入了两行,所以我搜索了很多问题/答案,发现googlechrome插件在这里导致了这个问题(json视图)图像
在禁用它并重新启动google浏览器后,它只插入了一行。为什么这个插件会有这种问题?最后,我检查了http请求,正如预期的那样,只有一个请求。
图像在此
你知道为什么会有这种行为吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题