Good evening everyone, while studying i came across PHP if, else and elseif statements. I am trying to put if and else to use but the IF statement is really not working. Maybe I'm missing a tiny detail. The code is suposed to check if the username is already in the table and then skip. Any help will be greatly appreciated. If this question has been answered here, kindly point me to teh URL
<?php
include 'inc.dbcon.php';
// Report all errors
error_reporting(E_ALL);
$username = mysqli_real_escape_string($con, $_POST['username'][0]);
$school = mysqli_real_escape_string($con, $_POST['school'][0]);
$candname = $_POST['candname'];
$query = mysqli_query($con, "SELECT * FROM parlia_votes WHERE username='".$username."'");
if(mysqli_num_rows($query) > 0) {
?>
<script>
alert('Vote has already been cast.');
window.location = 'logout.php';
</script>
<?php
}
else
foreach ($_POST['candname'] as $candname) {
$query = sprintf("INSERT INTO parlia_votes (username, school, candname) VALUES ('%s', '%s', '%s')", $username, $school, $candname);
?>
<script>alert('Thank you for casting your votes.');</script>
<script>window.location = 'logout.php'; </script>
<?php
$con->query($query);
}
?>
PS: I'm using a foreach loop to post to database. thanks