在一个查询中,INSERT和DUPLICATE都完成了他们的工作

In my code i have this query:

$this->mysqli->query(
    INSERT INTO `address` SET 
    `name` = '$street', `created_at` = '$timestamp' 
    ON DUPLICATE KEY UPDATE 
    `updated_at` = '$timestamp'");

But when program get same name name value he insert new row, and update old in same time. I done ADD INDEX(name); But still I get duplicate values, that I don't want.

I tried with preparing statement and binding param, but same thing.

You can set unique index for field(s) in the table and send query like this:

REPLACE INTO tablename (a,b,c,d) VALUES (?,?,?,?);

You need to add a unique key for name. Instead of doing:

create index idx_address_name on address(name);

You need:

create unique index idx_address_name on address(name);

You can declare a unique constraint in other ways as well, such as using the unique keyword in a create table or alter table statement.

With this, your code should work as you expect it to.