mysql查询函数插入重复行


Mysqli query function insert duplicate lines

我一直试图插入一个新的行mysql数据库,但insert(){…}函数插入了重复的行。

我也尝试了几种方法来插入,但它不工作。所有方法都插入了重复的行。

我该如何解决这个问题?你知道吗?

谢谢你的帮助&建议。

protected $db;
public function __construct() {
    try {
        $this->db = new mysqli('localhost', 'root', '', 'trigger');
        $this->db->set_charset("utf8");
    } catch (mysqli_sql_exception $e) {
        throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
    }
}
public function select($table, $rows = "", $where = "", $return_type = "") {
    if (empty($rows)) {
        $rows = "*";
    }
    if (empty($where)) {
        $where = "";
    } else {
        $where = "where " . $where;
    }
    try {
        $query = $this->db->query("SELECT $rows FROM $table $where");
        if ($return_type == "json") {
            return json_encode($query);
        } else {
            return $query;
        }
    } catch (mysqli_sql_exception $exc) {
        return $exc->getMessage();
    }
}
public function insert($table, $params) {
    $_keyArr = array();
    $_valueArr = array();
    foreach ($params as $key => $value) {
        $_keyArr[] .= $key;
        $_valueArr[] .= $value;
    }
    $keys = implode("`,`", $_keyArr);
    $values = implode("','", $_valueArr);
    $query = $this->db->query("INSERT INTO `$table` (`$keys`) VALUES('$values')");
    try {
        return $query;
    } catch (mysqli_sql_exception $ex) {
        return $ex->getMessage();
    }
}

-------------对matt的回答-------------

我用下面的代码调用:

    if ($this->db->insert("table_name", array("path" => "test", "flow_name" => "test"))) {
        echo 'ok';
    } else {
        echo 'not ok';
    }

-------------对帕维尔的回答-------------

"REPLACE INTO"无效。

用REPLACE INTO代替INSERT INTO

$query = $this->db->query("REPLACE INTO `$table` (`$keys`) VALUES('$values')");