dousong5161 2015-12-02 07:03
浏览 22

Php检查同一个表中的2个值[关闭]

How to check if the 2 values are exist for example

=============\
idno   || syearid
c23        2
c23        3

Like in the image below:

enter image description here

If the idno c23 exist and syearid 2 exist then it will message that this idno and syearuid exist. But when I save idno c23 and syearid 4 then it will be save because it is the same idno but not the same syearid. How should I do?

<?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,school_year where idno = '$idno' AND student.syearid = school_year.syearid AND school_year.from_year like $YearNow")or die(mysql_error());
    $count1 = mysql_num_rows($sql1);

     if ($idno AND $YearNow == $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'>
";
    echo "alert('Successfully Added.');
";
    echo "window.location = 'addusers.php';";
    echo "</script>";
    }

?>
  • 写回答

2条回答 默认 最新

  • dongzhan2029 2015-12-02 07:11
    关注

    generate an array then you would be able to perform the checks in logic, for example check for idno id 2.

    I hope this is what your trying to ask.

    if(in_array(2, $idno)){
        // echo id 2 is received
    }
    
    评论

报告相同问题?