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
, CONSTRAINTfk_referals_users1
FOREIGN KEY (users_id
) REFERENCESusers
(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();
}