From 14ba3f05c8ba134cacc884cca14d984d8fd86ade Mon Sep 17 00:00:00 2001 From: "Evan G." Date: Mon, 10 Jun 2024 14:47:35 -0500 Subject: [PATCH] Don't Allow Duplicated messages --- submit-sql.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/submit-sql.php b/submit-sql.php index 23a7fca..570e2d2 100644 --- a/submit-sql.php +++ b/submit-sql.php @@ -75,24 +75,37 @@ function setupdb($dbh) { )'); } # Adding an Item -function additem($ndata, $dbh) { +function additem($ndata, $dbh, $newURL) { + ## Check for Duplicates + $dupmessagech = "SELECT count(*) FROM messages where message='$ndata->message'"; + $dupmessage = $dbh->query($dupmessagech)->fetchColumn(); + + if($dupmessage > 0) { + echo "Another message with this text already exists"; + header('Refresh:2; url=' . $newURL ); + exit(); + } else { $sql = 'INSERT INTO messages (id, name, date, message, mlength, nlength) VALUES (?, ?, ?, ?, ?, ?)'; $sth = $dbh->prepare($sql); $sth->execute(array( - null->id, + null, $ndata->name, $ndata->date, $ndata->message, $ndata->mlength, $ndata->nlength )); + header('Refresh:2; url=' . $newURL ); + echo "Your submisson has been added"; + exit(); + } } # Setup the Database (If table does not already exist) setupdb($dbh); # Add the Item -additem($ndata, $dbh); +additem($ndata, $dbh, $newURL); ?>