From d1f0b54ff5e1d1ddb7d48c39a6aec9f588d7305b Mon Sep 17 00:00:00 2001 From: mischa Date: Wed, 24 Aug 2022 17:34:31 +0000 Subject: [PATCH] Exception handling for INSERT --- index.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index a9e6818..5e0c5f0 100644 --- a/index.php +++ b/index.php @@ -62,11 +62,13 @@ function generate_short($url, $dbh) { $sth->execute(); $loop++; } - $sth = $dbh->prepare("INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES (?, ?, ?, '0')"); - $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH); - $sth->bindParam(2, $url, PDO::PARAM_STR); - $sth->bindParam(3, $clientip, PDO::PARAM_STR, 255); - if (!$sth->execute()) { + try { + $sth = $dbh->prepare("INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES (?, ?, ?, '0')"); + $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH); + $sth->bindParam(2, $url, PDO::PARAM_STR); + $sth->bindParam(3, $clientip, PDO::PARAM_STR, 255); + $sth->execute(); + } catch (PDOException $e) { $hash = "ERROR
Failed to insert hash!"; } }