function add_position($bid, $positions) { rem_position($bid); foreach ($positions as $key => $values) { if (!empty($values) && $values !== " ") { mysql_query("INSERT INTO `online_elections`.`positions` (`ballot_id`, `position_name`, `position_rank`) VALUES ('{$bid}', '{$values}', '{$key}')"); } } }
<?php if (isset($_GET['bid'], $_POST['positions'])) { $errors = ""; if (empty($_POST['positions'])) { $errors = "All positions removed."; rem_position($_GET['bid']); header('Location: ballot.php?bid=' . $_GET['bid']); } else { $positions = explode(',', $_POST['positions']); foreach ($positions as $key => $value) { $positions[$key] = trim($value); } echo "<br />"; add_position($_GET['bid'], $positions); header('Location: ballot.php?bid=' . $_GET['bid']); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Voting Site - Add Position</title> <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen" /> <link href="misc/css/index.css" rel="stylesheet" media="screen" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="bootsrap/assets/css/bootstrap-responsive.css" rel="stylesheet" /> </head> <body>