在更新之前检查列是否为零

How do i insert into a database checking if the column is zero before inserting into it

this is what i have tried but after updating it still insert a new row with the last inserted ID but rather i just want it to update without inserting

    $lastID = mysqli_insert_id($DBcon);
    $query2=$DBcon->query("SELECT * FROM mergeing"); 
    $compare_value = "0";
    if($row = $query2->fetch_array()) {
        $merger = "INSERT into mergeing(donator_1) VALUES ('$lastID')";
        if($row['donator_2'] !== "$compare_value") {
            if ($DBcon->query($merger)) {
                echo "success second";
            }
        }
    }
    while ($row = $query2->fetch_array()) {
        $idd= $row["_id"];
        $merg2 = "UPDATE mergeing SET donator_2='".$lastID."'  WHERE _id=$idd";
        if($row['donator_2'] === "$compare_value") {
            if ($DBcon->query($merg2)) {
                echo "success";
            }
        }
    } 

It seems you want to check if there is a record that has a specific donator_2 value, and if so, you want it updated. If no such value is present, you want to insert a new record.

Then your code could look like this:

$lastID = mysqli_insert_id($DBcon);
$compare_value = 0;
$DBcon->query("UPDATE mergeing SET donator_2 = $lastID WHERE donator_2 = $compare_value");
if ($DBcon->affected_rows) {
    echo "success: updated";
} else {
    $DBcon->query("INSERT into mergeing(donator_1) VALUES ($lastID)");
    echo "success: inserted";
}

Depending on your actual use case, but you might want to also set the donator_2 value when you insert the new record.

Please note that if the $compare_value is determined by user input or some other source that you cannot predict, then you should use prepared statements, as otherwise the code is open to SQL injection.