ARTICLE AD BOX
I have a login page and ballot. To ensure no one is voting more than once, the checked names on the ballot are stored in the database along with their login username and corresponding ID number. For most ballots cast this work just fine.
However, there have been ballots cast where the votes are stored but the username is blank and the ID number is 0. Zero is not an assigned value.
Following is the code for process.php, where votes are submitted. My login page sets $_SESSION['username'] and $_SESSION['user_id'] after authenticating a user, and prevents users from logging in if they have already voted.
<?php require_once('../Connections/pdo-connsunnygrove.php'); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); session_start(); $dbusername = $_SESSION["username"]; $userid = $_SESSION["user_id"]; $checkbox1 = $_POST['name']; $chk=""; foreach($checkbox1 as $chk1) { $chk.= $chk1.","; } $conn = mysqli_connect($hostname_sunnygrove, $username_sunnygrove, $password_sunnygrove, $database_sunnygrove); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } try { $sql = "INSERT INTO election (name, nominator, userid) VALUES('$chk', '$dbusername', '$userid')"; if (mysqli_query($conn, $sql)) { echo 'Thank you for voting.'; } } catch (Exception $e){ echo "Error:You have already voted. Contact Pete Leipzig if you have made a mistake and wish to remove your previous ballot."; } mysqli_close($conn); ?>