用于检查重复条目的SQL OR条件

时间:2014-09-20 08:01:32

标签: php mysql

伙计们,我需要一点帮助。我需要检查数据库中的重复条目。我需要检查重复的电话条目或重复的电子邮件条目。这是sql。这是对的吗?因为它不起作用。

if(isset($_POST['submit']))
            {
                foreach($_POST as $key=>$val)
                ${$key}=addslashes($val);

                $contdupcheck = "select * from `contact` where `email` = '$email' OR `phone` = '$phone'";
                $contdupresult = mysql_query($contdupcheck);
                if(mysql_num_rows($contdupresult)==2)
                {
                    ?>
                    <script type="text/javascript">         
                    notification('You have already sent something twice!','error');
                    </script> 
                    <?php
                } 
                else 
                {

                    $contquery = "INSERT INTO `contact` (`name`, `email`, `phone`, `message`) VALUES ('$name', '$email', '$phone', '$contmessage')";
                    $contresult = mysql_query($contquery);
                    if($contresult)
                    {
                        ?>
                        <script type="text/javascript">         
                        notification('Thank you for your message!','success');
                        </script>           
                        <?php
                    }
                    else 
                    {
                        ?>
                        <script type="text/javascript">         
                        notification('There was an error. Please try after some time!','error');
                        </script>  
                        <?php
                    }
                }
            }

2 个答案:

答案 0 :(得分:0)

  

将您的查询分为两部分AS @suchit说

$contdupcheck = "select * from `contact` where `email` = '$email' OR `phone` = '$phone'";
   REPLACE IN TO

$contdupcheck = "select * from `contact` where `email` = '$email'";
$contdupcheck1 = "select * from `contact` where `phone` = '$phone'";
 $contdupresult = mysql_query($contdupcheck);
 $contdupresult1 = mysql_query($contdupcheck1);
AND NOW MATCH
 if(mysql_num_rows($contdupresult)==2 || mysql_num_rows($contdupresult1)==2)

答案 1 :(得分:0)

评论:如果需要电子邮件和电话,它只会产生预期的结果。如果他们是可选的让我知道,我会更新我的答案。而且你不需要为此运行两个查询。谢谢。如果它不起作用,请告诉我。

$contdupcheck = "select * from `contact` where `email` = '$email' OR `phone` = '$phone'";

将此行更改为此

$contdupcheck = "select * from contact where email = '$email' AND phone = '$phone'";