如果声明不起作用

I have 2 sql queries in my code where the second one is wrapped around an if statement which should only execute if a certain post value exists.

But I still get the following error message:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (database1.table2, CONSTRAINT fk_referals_users1 FOREIGN KEY (users_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

Clearly that if statement is not working, if it was, it would not get to the second sql query.

Here is the problem section of the script:

$STH = $DBH -> prepare( "insert into database1.table1 (display_name, email, password) values ( :display_name, :email, :password )" );

$STH -> bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );

$STH -> execute();

if( isset( $_POST['referer'] ) or ( $_POST['referer'] != null ) or ( $_POST['referer'] != "" ) ) {
    $STH = $DBH -> prepare( "insert into database1.table2 ( username, status, users_id ) values ( :username, :status, :users_id )" );

    $strStatus = 1;

    $STH -> bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
    $STH -> bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
    $STH -> bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );

    $STH -> execute();
}

The IF statement checks whether a certain POST value is set. Your foreign key constraint checks whether the value in the POST field is present in the users table. These two conditions are not related. The POST field can be set with a value that is not present in the users table. You should check whether the value is present in the users table (do a COUNT maybe?) before trying to insert (this depends on your setup, of course)

Also, you should use empty() for your IF condition as isset() will trip your condition to TRUE even if the value is empty. Better to use

if (!empty($_POST['referer'])) {
   //your code here
}

One of your if conditions is resolving to true.

Break your compound if statement into 3 nested ifs and add an echo at each level to see which one(s) are failing.

null and "" are different values, so if it equals null it will not equal "" and the IF will pass true. I recommend this if ( $_POST['referer'] && strlen($_POST['referer']) > 0 ). This will check if it is null or not (if it is not set it is null) and if there is a string with a length greater than 0