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
if (isset($_POST['login'])) $email = $_POST['email']; $password = $_POST['password'];
flowchart TD A[Voter] --> B[Browser] C[Admin] --> B B --> D[Apache Web Server] D --> E[PHP Scripts<br>Login, Vote, Results] E --> F[MySQL Database<br>User Data, Votes, Candidates] F --> E E --> D D --> B B --> A B --> C ''; public function getConnection() $this->conn = null; try
The PHP-MySQL combination provides an excellent foundation for an online voting application due to its key strengths:
users
CREATE TABLE candidates ( id INT AUTO_INCREMENT PRIMARY KEY, election_id INT NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, photo VARCHAR(255), created_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (election_id) REFERENCES elections(id) ON DELETE CASCADE ) ENGINE=InnoDB;
host = $_ENV['DB_HOST'] ?? '127.0.0.1'; $this->db_name = $_ENV['DB_NAME'] ?? 'voting_system'; $this->username = $_ENV['DB_USER'] ?? 'root'; $this->password = $_ENV['DB_PASS'] ?? ''; public function getConnection() $this->conn = null; try $this->conn = new PDO( "mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4" ] ); catch(PDOException $exception) error_log("Connection error: " . $exception->getMessage()); die("Database connection failed. Please check your configuration."); return $this->conn; ?> Use code with caution. B. Secure Vote Submission ( submit_vote.php ) Database Schema Design ( database.sql )
In the digital age, conducting secure, efficient, and transparent electionsβwhether for a student council, a housing society, or a small organizationβhas become essential. An eliminates paper ballots, reduces manual errors, and increases voter turnout.
Fork a repo, fire up XAMPP portable, and start voting! Fork a repo
voting-system-php/ β βββ config/ β βββ db.php # Database connection settings β βββ assets/ β βββ css/ # Custom styling β βββ js/ # Form validation and charts β βββ admin/ β βββ index.php # Admin login β βββ dashboard.php # Overview of votes and statistics β βββ candidates.php # CRUD operations for candidates β βββ positions.php # Manage election categories β βββ index.php # Voter login page βββ ballot.php # Voting interface βββ submit_vote.php # Logic for processing ballots βββ README.md # GitHub documentation βββ database.sql # Database schema import file Use code with caution. 3. Database Schema Design ( database.sql )