After post request i need to:

1. Check if u\_1 exists on table and if it is in table insert u\_2 where u\_1 in not null
2. If u\_1 is not exists insert u\_1

Here is my **PHP** code

```
if ( isset( $_POST["cards"],$_POST["user"],$_POST["cardvalues"],$_POST["tablekey"] ) ) {

    $cards = $_POST["cards"];
    $user = $_POST["user"];
    $cardvalues = $_POST["cardvalues"];
    $tablekey = $_POST["tablekey"];

    $checkIfNull = "SELECT u_1 FROM tables";
    $insertIntoU_2 = "INSERT INTO tables (u_2) VALUES (?) WHERE u_1 IS NOT NULL";
    $insertIntoU_1 = "INSERT INTO tables (u_1,all_cards,card_value,table_key) VALUES (?,?,?,?)";

    if ($stmt = $conn->prepare($checkIfNull)) {
        if ($stmt->execute()) {
            $result = $stmt->get_result();
            if ($result->num_rows > 0) {
                if ($stmt = $conn->prepare($insertIntoU_2)) {
                    $stmt->bind_param("s",$user);
                    if ($stmt->execute()) {
                        echo "user 2 is in table";
                    }
                }
            }else{
                if ($stmt = $conn->prepare($insertIntoU_1)) {
                    $stmt->bind_param("ssss",$user,$cards,$cardvalues,$tablekey);
                    if ($stmt->execute()) {
                        echo "user 1 is in table";
                    }
                }
            }
        }
    }

```