This is the EDITED version of my first post because it seemed to be not enough clear the first one.
I want to insert or update if exists depending on a SELECT query result. This is my code working in a standard way:
$query = "SELECT * FROM table1 WHERE id1 =".$id1." AND id2=".$id2;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$query = "UPDATE table1 SET id3=".$id3." WHERE id1 =".$id1." AND id2 = ".$id2." LIMIT 1";
$result = mysqli_query($conn, $query);
} else {
$query = "INSERT INTO table1 (id1, id2, id3) VALUES ($id1, $id2, $id3)";
$result = mysqli_query($conn, $query);
}
id1, id2, id2 values can be repeated but under one condition (id1 and id2 can't be repeated at the same time.
For example:
a new row can be inserted only in a situation like this:
if this one already exists in the table1
id0=0 id1=5 id2=10 id3=15
then the next one would be:
id0=1 id1=6 id2=10 id3=15
or
id0=1 id1=5 id2=11 id3=15
a row would be updated when
if this one already exists in the table1
id0=0 id1=5 id2=10 id3=15
and the next one would be:
id0=1 id1=5 id2=10 id3=20
as you notice the id1 and id2 is the same as the one already in the table, so id3 would be updated.
Also my table1 looks like this:
CREATE TABLE table1 (
`id0` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id1` int(11) NOT NULL,
`id2` int(11) NOT NULL,
`id3` int(11) NOT NULL
) ENGINE=InnoDB;
MY QUESTION IS: Is it possible to combine the above 3 queries into one using "insert on duplicate key update" or any other way?