Here's my data structure of each table:
I have a problem that I need help with. I have a table student_votes and student. My problem is the second condition which is the checking the student if he/she already voted. Because when I run my codes it always go to the plsvote.php even when it's not supposed to.
student_votes table has:
(id(pri),candid(foreign),idno(foreign),syearid(foreign))
Here's my code:
<?php
//Start session
session_start();
//Array to store validation errors
//Connect to mysql server
include('connect.php');
//Function to sanitize values received from the form. Prevents SQL injection
//Sanitize the POST values
if (isset($_POST['login'])){
$idno = mysql_real_escape_string($_POST['idno']);
$password = mysql_real_escape_string($_POST['password']);
$position = mysql_real_escape_string($_POST['user_type']);
$YearNow=Date('Y');
//checking student idno and password
$sql1 = "SELECT * FROM student,school_year WHERE idno = '$idno' AND password = '$password' AND school_year.syearid = student.syearid AND school_year.from_year like $YearNow" ;
$result = mysql_query($sql1) or die();
$row = mysql_fetch_array($result);
$num_row = mysql_num_rows($result);
//checking if the student has been voted
$sql2 = "SELECT * FROM student,studentvotes WHERE student.idno = studentvotes.idno AND syearid = $YearNow" ;
$result1 = mysql_query($sql2) or die();
$row1 = mysql_fetch_array($result1);
$num_row = mysql_num_rows($result1);
if ($row['user_type'] == "1"){
// $query = mysql_query ("INSERT INTO user_log VALUES('','$idno',NOW(), 'Login') ") or die(mysql_error());
header('location:admin/index.php');
} else if ($row['user_type'] == "3") {
//here is the part where I would check if the student already voted or not
if ($num_row > 0) {
$sql_c = "SELECT * FROM student WHERE idno = '$idno' AND password='$password' ";
$result1 = mysql_query($sql_c) or die(mysql_error());
while($row2=mysql_fetch_array($result1)){
$_SESSION['SESS_COURSE'] = $row2['progid'];
$_SESSION['SESS_MEMBER_ID'] = $idno;
header('location: plsvote.php');
//$query = mysql_query ("INSERT INTO user_log VALUES('$idno',NOW(), 'Login') ") or die(mysql_error());
}
} else {
header('location: notification.php');
}
} else if ($row['user_type'] == "2"){
// $query = mysql_query ("INSERT INTO user_log VALUES('','$idno',NOW(), 'Login') ") or die(mysql_error());
header('location:admin/officerpanel.php');
//$_SESSION['admin'] = $idno;
} else {
echo "<script type='text/javascript'>\n";
echo "alert('Username or Password incorrect!, Please try again.');\n";
echo "window.location = 'index.php';";
echo "</script>";
exit();
}
}
?>