FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to check values with foreign key in PHP
#1
Help me I have a problem in terms of checking using AND. I have a table name student it has idno and syearid (syearid table belong to table school_year) school_year table has (syeardid,from_year,to_year). For example form_year means 2015 and to_year means 2016. so 2015-2016.. My problem is hot to check if that idno and syearid has exist on the database. It's okey if the idno is redundant beacause I will based it on the syearid. IF idno 'crr' and registered in syearid = 2 which is 2015 and I will Register again same idno but not the same syearid It should be save because it is not the same

<?php


if (isset($_POST['save'])){
$stud_id= $_POST['stud_id'];
$idno = $_POST['idno'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$middlename= $_POST['middlename'];
$year= $_POST['year'];
$dept_id = $_POST['dept_id'];
$progid = $_POST['progid'];
$user_type = $_POST['user_type'];
$password= $_POST['password'];
$syear= $_POST['syearid'];
$YearNow=Date('Y');

$sql1 = mysql_query("SELECT * FROM student where idno = '$idno' ")or die(mysql_error());
$count = mysql_num_rows($sql1);

$sql2 = mysql_query("SELECT * FROM student,school_year where student.syearid = school_year.syearid AND school_year.from_year like $YearNow")or die(mysql_error());
$count1 = mysql_num_rows($sql2);

$yeah = $count AND $count1;
//check based on idno student.syearid = school_year.syearid AND school_year.from_year like $YearNow
if ($count1 >1 ) {
echo"idno $idno has already exist with an exact current year $YearNow";

}
else{

// query
$sql = "INSERT INTO student VALUES ('$stud_id','$idno','$dept_id','$progid','$syear','0','$lastname','$firstname','$middlename','$year','$password','$user_type')";
$result = mysql_query($sql) or die(mysql_error());



echo "<script type='text/javascript'>\n";
echo "alert('Successfully Added.');\n";
echo "window.location = 'addusers.php';";
echo "</script>";
}

?>

My problem to my code is when I register idno 'crr' and hes syearid of from_year is 2015 and when I registered again to a different from_year it will always message that idno exist with exact year and dat's wrong. Please really need help
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)