I'm trying to learn how to make a registration form that publishes data to a database. I couldn't get the code from a tutorial to work until I stripped it down. It now works OK, except that if I refresh the page (to remove the values I typed in so I can type in another entry), it republishes the original data to the database table.
In other words, if I type in the values Kelly Kau for firstname and lastname, then hit the Submit button, Kelly and Kau are entered in the database table. But when I refresh the page, another row with Kelly and Kau is added.
Is there a way to stop this? I'm working with PHP and MySQL. The tutorial I'm trying to copy is jQuery. Eventually, I may add some AJAX, too.
<form id="signupform" autocomplete="off" method="post" action="" novalidate>
<table>
<tr>
<td class="label"><label id="lfirstname" for="firstname">First Name</label></td>
<td class="field"><input id="firstname" name="firstname" type="text" value="" maxlength="100"></td>
<td class="status"></td>
</tr>
<tr>
<td class="label"><label id="llastname" for="lastname">Last Name</label></td>
<td class="field"><input id="lastname" name="lastname" type="text" value="" maxlength="100"></td>
<td class="status"></td>
</tr>
<tr>
<td class="label"><label id="lsignupsubmit" for="signupsubmit">Signup</label></td>
<td class="field" colspan="2"><input id="signupsubmit" name="signup" type="submit" value="Signup"></td>
</tr>
</table>
</form>
<?php
include('ajax-database/config.php');
$pdo = connect();
try {
$sql = "INSERT INTO g1_members (firstname, lastname) VALUES (:firstname, :lastname)";
$query = $pdo->prepare($sql);
$query->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
$query->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);
$query->execute();
} catch (PDOException $e) {
echo 'PDOException : '. $e->getMessage();
}
?>