How to capture Mysql error 1452 and present message to user?

Asked

Viewed 69 times

0

I have the function below that makes the insertion of a new record in a database.

// INSERE UM NOVO REGISTRO NO BANCO DE DADOS
function save($table = null, $data = null) {
	$_SESSION['message'] = null;
	$_SESSION['type'] = null;
	$database = open_database();
	$columns = null;
	$values = null;
	foreach ($data as $key => $value) {
		$columns .= trim($key, "'") . ", ";
		$values .= "'$value', ";
	}
	$columns = rtrim($columns, ', ');
	$values = rtrim($values, ', ');
	$sql = "INSERT INTO " . $table . " ($columns) " . "VALUES " . "($values);";
	//echo $sql; //usado apenas para depurar
	try {
		$database->query($sql);
		if (!$database->errno == 1452) {
			echo "<script type='text/javascript'>alert('Existe uma Informação duplicada no Banco de Dados');</script>";
		}
		$_SESSION['message'] = "Registro inserido com sucesso!";
		$_SESSION['type'] = 'success';
	} catch (Exception $e) {
		$_SESSION['message'] = 'Nao foi possivel realizar a operacao!';
		$_SESSION['type'] = 'danger';
	}
	close_database($database);
}

I am unable to get the Mysql error code and present a message to the user. In this case here, problems with reference.

I used this link here from the forum as guidance, but it didn’t work: Handle Mysql error

Where am I going wrong?

  • but what would be the error returned with these settings, what error you are getting ?

  • @Michaelcosta, when I file a form, the data is not recorded. Then I blur the line where I do an echo $sql and with that the system shows me the complete sql statement. If I copy this instruction and run in the database directly, Mysql reports missing integrity with 1452 error.

  • put here the full statement that is sent in $sql..

  • 1

    As far as I understand you want to show an msg to the user about the error... but you know that this error, is because you are trying to register something in the bank, which depends on a key that is probably not registered ? a key Foreign.. you really just want to treat the same error ?

  • type to tell the user, that first he must register 'ABC' and then register 'XYZ'

  • even @Michaelcosta you can see that I tried to catch the error in the code but it did not work.

Show 1 more comment

1 answer

1


Bro.. I understand your problem.. you made a reversal of yours true false, for false true. when you added if(! exclamation) inside your if. so it’s not working.

change this

if (!$database->errno == 1452) {
            echo "<script type='text/javascript'>alert('Existe uma Informação duplicada no Banco de Dados');</script>";
        }

therefore

if ($database->errno == 1452) {
            echo "<script type='text/javascript'>alert('Existe uma Informação duplicada no Banco de Dados');</script>";
        }

I believe that’s the problem. vlw.

Remembering that this error is not duplicity, as you refer in the Alert of your javascript. and yes that "X" can only be registered after "Y" is registered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.