Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        4年前关闭。
                                                                                            
                
        
例如,如何检查两个值是否存在

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


如下图所示:

php - PHP检查同一张表中的2个值-LMLPHP

如果idno c23存在且syearid 2存在,则它将提示此idnosyearuid存在。但是,当我保存idno c23syearid 4时,它将被保存,因为它是相同的idno但不是相同的syearid。我应该怎么做?

<?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'>\n";
    echo "alert('Successfully Added.');\n";
    echo "window.location = 'addusers.php';";
    echo "</script>";
    }

?>

最佳答案

您可以将“ idno”和“ syearid”作为表的组合唯一键

ALTER TABLE student ADD UNIQUE KEY (idno, syearid);

它将idno和syearid的组合视为唯一实体,并且数据库在插入重复项时会产生错误。

关于php - PHP检查同一张表中的2个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34036742/

10-09 12:33