Strange MySQL generated no error

Below is part of a form I’m working on. I have two questions about capturing MySQL insertion errors.

  1. I have an odd failure to trigger an error message. In the example below, two values are being inserted into a MySQL database from variables in the PHP script. A change in the var name alerted me to the failure to capture the error. Below I’ve exaggerated the mistake with extra KKKKKKKKKKs. In the example, this mistyped var does not generate an error to location A, and instead skips to B. Why?

/* CODE BLOCK FOR $dbSuccess_1... */
/* CODE BLOCK FOR $dbSuccess_2... */
$insertPermsn = "INSERT INTO
	permissions(
		mailing_list,
		email_id
	)VALUES(
		'$mailingOKKKKKKKK',
		'$lastMysqlID'
	)";
$dbSuccess_3 = mysql_query($insertPermsn,$connectID) or die ("ERROR - Unable to save".error_get_last().mysql_error($connectID));
	
if(!($dbSuccess_1 && $dbSuccess_2 && $dbSuccess_3)){
	/* IF NOT SAVED, RECORD THE ERROR TO THE ERROR ARRAY 	*/
	/*      A	*/
	$Errors[] = 'Your data was not saved. There is a problem with the server. Please try back later';
}else{
	/* 	B      */
	/* IF SAVED, THEN CONTINUE HERE 	*/

  1. I’m also working to manage my errors and tried adding the error array directly to the die() exception. This is not working. Any reason die() won’t load the string into this errors array? Or is it tied to the other problem in some way?
$dbSuccess_3 = mysql_query($insertPermsn,$connectID) or die (($Errors[]="ERROR - Unable to save").error_get_last().mysql_error($connectID));

With the current logic of

 
if(!($dbSuccess_1 && $dbSuccess_2 && $dbSuccess_3)){

your script will go to location A only if all of dbSuccess_1,2,3 are all false

I suspect what you really need is -

 
if(!$dbSuccess_1 || !$dbSuccess_2 || !$dbSuccess_3){ 
 
//spit out error message and then
 
die();
 
} 
 
//no errors - so continue on processing

To test error logic, I usually add a few chars to a column name in the query or change $conn to $conxx or something similar.

Nope. I still don’t get this…
>The var DOES exist.
I understand php does not require a var to be initialized, so technically it does exist in PHP. Yet MySQL is receiving instructions to receive data from two vars and insert this in a table with a field. If the var has no data, and the field will not accept null value, I expect MySQL to create the error.

Its certainly a moot point. I’ve identified a problem, corrected it and it won’t happen again. Yet, in my self-taught programming experience, there’s nothing worse then a problem without an error response. I’ve never met a black box programmer.

>Let’s say that $a = ‘test’.
>As you know, PHP substitutes variables between double quotes with their value, so:
>“$a” would become “test”
>but
>“$aa” would become “testa”
I know only too well…

X

Like xtiansimon says, you’re wrong here.

!$dbSuccess_1 || !$dbSuccess_2 || !$dbSuccess_3
is equal to
!($dbSuccess_1 && !$dbSuccess_2 && !$dbSuccess_3)

See: http://en.wikipedia.org/wiki/De_Morgan's_laws

Like dreamconception already explained, mysql threw no error because there was none.

Your statement is not correct. Try fiddling with this and you’ll see what I mean:

<?php
$a = 1;
$b = 1;
$c = 1;
if (!($a && $b && $c)){
	echo 'One or all false';
}else{
	echo 'All true';
}
?>

The var DOES exist.

Let’s say that $a = ‘test’.

As you know, PHP substitutes variables between double quotes with their value, so:
“$a” would become “test”

but
“$aa” would become “testa”

You won’t get an error because $aa doesn’t exist, $a does and the rest is seen as a simple string.

Clear? :slight_smile:

That IS the first problem. I don’t understand why there is no error in this case. If the var does not exist, why no error? (eg the var in the script is $a and you’ve mistakenly passed $aa through INSERT).

And the second question is about the array() in the die() statement…

Chris

  1. You do not generate an error because the K’s is just part of a variable. To generate an error please do this;

/* CODE BLOCK FOR $dbSuccess_1... */ 
/* CODE BLOCK FOR $dbSuccess_2... */ 
$insertPermsn = "INSERT INTO 
    permissions( 
        mailing_list, 
        email_id 
    )VALUES( 
        '$mailing'OKKKKKKKK, 
        '$lastMysqlID' 
    )"; 
$dbSuccess_3 = mysql_query($insertPermsn,$connectID) or die ("ERROR - Unable to save".error_get_last().mysql_error($connectID)); 
     
if(!($dbSuccess_1 && $dbSuccess_2 && $dbSuccess_3)){ 
    /* IF NOT SAVED, RECORD THE ERROR TO THE ERROR ARRAY     */ 
    /*      A    */ 
    $Errors[] = 'Your data was not saved. There is a problem with the server. Please try back later'; 
}else{ 
    /*     B      */ 
    /* IF SAVED, THEN CONTINUE HERE     */ 

  1. I do not really understand exactly what you want.

Also Kalon suggest an issue with your logic, all of the datbase calls has to fail for the error to load, is that how you want it to be?