In the digital age, the demand for efficient, secure, and accessible voting mechanisms has grown significantly. Traditional paper-based voting is often time-consuming, resource-intensive, and prone to human error or manipulation. To address these challenges, I developed an Online Voting System using PHP for server-side scripting and MySQL for database management. This project aims to provide a streamlined, user-friendly platform for conducting elections in academic institutions, small organizations, or local communities. The complete source code is available on GitHub: https://github.com/yourusername/online-voting-system-php.
session_start(); if (!isset($_SESSION['user_id'])) header('Location: login.php'); exit();$voter_id = $_SESSION['user_id']; $candidate_id = $_POST['candidate_id'];
// Check if already voted $check = "SELECT is_voted FROM users WHERE id='$voter_id'"; $result = mysqli_query($conn, $check); $user = mysqli_fetch_assoc($result); Essay: Development of an Online Voting System Using
if ($user['is_voted'] == 0) // Insert vote $insert_vote = "INSERT INTO votes (voter_id, candidate_id, vote_date) VALUES ('$voter_id', '$candidate_id', NOW())"; mysqli_query($conn, $insert_vote);
// Update candidate vote count $update_candidate = "UPDATE candidates SET vote_count = vote_count + 1 WHERE id='$candidate_id'"; mysqli_query($conn, $update_candidate); // Mark user as voted $update_user = "UPDATE users SET is_voted=1 WHERE id='$voter_id'"; mysqli_query($conn, $update_user); echo "Vote cast successfully!";
else echo "You have already voted!";
Most Core PHP/MySQL voting projects found on GitHub share a standard workflow: else echo "You have already voted
admin table (credentials).voters table (ID, name, password, status: voted/pending).candidates table (name, position, photo).votes table (voter_id, candidate_id, timestamp).status == 'voted'. If yes, redirect to results page. If no, show ballot.votes table and flips voter status to 'voted'.htdocs (XAMPP) or www (WAMP).voting_system.database.sql file.config/db.php.http://localhost/online-voting-system/| Threat | Solution |
|--------|----------|
| Double voting | UNIQUE(voter_id, election_id) + has_voted flag |
| SQL Injection | Prepared statements in every query |
| Session hijacking | Regenerate session ID after login, set short session timeout |
| Fake votes via URL | Validate election status and user role on every vote request |
| XSS | htmlspecialchars() on all candidate names and party names |
| CSRF | Add CSRF tokens on vote submission forms |