candidate_id $conn->beginTransaction(); try // Check if already voted $stmt = $conn->prepare("SELECT voted_status FROM users WHERE id = :id"); $stmt->execute(['id' => $voter_id]); $user = $stmt->fetch(); if ($user['voted_status'] == 1) throw new Exception("You have already cast your vote."); // Insert votes into database foreach ($choices as $position_id => $candidate_id) $vote_stmt = $conn->prepare("INSERT INTO votes (voter_id, position_id, candidate_id) VALUES (:voter, :pos, :cand)"); $vote_stmt->execute([ 'voter' => $voter_id, 'pos' => $position_id, 'cand' => $candidate_id ]); // Update voter status $update_stmt = $conn->prepare("UPDATE users SET voted_status = 1 WHERE id = :id"); $update_stmt->execute(['id' => $voter_id]); $conn->commit(); echo "Vote cast successfully!"; catch (Exception $e) $conn->rollBack(); echo "Error: " . $e->getMessage(); ?> Use code with caution. Enhancing Security in Digital Elections

PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, DB_USER, DB_PASS, $options); catch (PDOException $e) die("Database connection failed: " . $e->getMessage()); ?> Use code with caution. Secure Vote Processing Logic ( vote.php )

The system follows a :

A user-friendly dashboard to view candidates and cast a single vote per election.

: Tools to approve, reject, or import eligible voter lists via CSV files.

: Create a simple Windows Batch script ( start.bat ) in the root directory to launch the server and database instantly with one double-click. Example start.bat Implementation

This backend script processes cast votes. It implements transaction mechanisms to guarantee atomicity: either both the ledger update and ballot count succeed, or everything rolls back.

In this article, we have discussed an online voting system project in PHP and MySQL, including its features, source code, and GitHub repository. We have also provided a portable version of the project that can be easily deployed on any server. The online voting system project in PHP and MySQL is a secure, transparent, and convenient way to conduct elections.

One of the biggest hurdles for students is setting up the development environment. The version we are sharing is , meaning you can run it directly on a USB stick or a local XAMPP server without complex configuration dependencies.