I want to redirect to other page if INSERT failed because UNIQUE value is duplicated, I'm using PDO. Here's code:
try {
$hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$dbh = new PDO('mysql:host=localhost;dbname=Lime', 'root', '');
$stmt = $dbh->prepare("INSERT INTO `users` (username, password) VALUES (:username, :password);");
$stmt->bindParam('username', $_POST['username']);
$stmt->bindParam('password', $hashed_password);
$stmt->execute();
if (!$stmt) { header("Location: entry.php?reg&err"); }
$dbh = Null;
} catch (PDOException $e) { header("Location: entry.php?reg&err"); }
Here you can see I'm tring to use both of the ways threw $stmt is false on error, and try catch
Does any1 know why, and how to fix that?
After debugging for a while, I understand that SQL doesn't even return error, at least for PHP, but if i run same code by hand(as SQL request) then it returns error. Also DB doesn't change while I'm trying to INSERT from PHP with duplicate UNIQUE field. For unique values everything is file
My english is bad, I'll be really happy if somebody could edit my mistakes