Make INSERT Fail

How can I manually force this INSERT to fail…


	// Prepare statement.
	$stmt2 = mysqli_prepare($dbc, $q2);

	// Bind variables to query.
	mysqli_stmt_bind_param($stmt2, 'sssssss', $email, $activationCode, $salt, $hash,
									$firstName, $registerIP, $registerHostname);

	// Execute query.
	mysqli_stmt_execute($stmt2);

	// Verify Insert.
	if (mysqli_stmt_affected_rows($stmt2)==1){
		// Insert Succeeded.
		$_SESSION['resultsCode'] = 'ACCOUNT_MEMBER_ACCT_CREATED_2102';

	}else{
		// Insert Failed.
		$_SESSION['resultsCode'] = 'ACCOUNT_MEMBER_ACCT_FAILED_2102';
	}// End of VERIFY INSERT.

Everything I tried just produced a Fatal Error and so it wouldn’t even run before failing…

Thanks,

Debbie

I’m not a mysqli user, but try working it out this way:



// put the value into a variable for now
$result = mysqli_stmt_affected_rows($stmt2);

var_dump( $result ); // what does that show you in both the pass and fail situations?

if( $result === X ){  // now test for the positive value AND the type with the stricter "===" test

echo 'it was a pass';

}else{

echo 'it was a fail'; 

}

You are forking your code depending on a condition, yet you fail to establish what are the variables you are checking – that is a missing part of your coding strategy.

Once you adopt this kind of “debug as you go” practice, you will be freed from the tyranny of forever be scratching your head about what is going on inside PHP conditional checks.

Once sorted out you can then remove all the $result stuff, and simply test the value of mysqli_stmt_affected_rows as you did previously, then next time you are coming up to a fork in your code – check the variables out first, then write the sql statements etc.

In this specific case, what exactly is the situation where you would arrive at the insert, and it would fail? A duplicate key I would be a common scenario. So if you have a unique key on email for example, you could try to create two accounts with the same email address.

Good point.

Debbie