伙计们,我需要一个小小的帮助。我需要检查数据库中是否有重复的条目。我需要检查重复的电话条目或重复的电子邮件条目。下面是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
}
}
}发布于 2014-09-20 16:20:57
只需将查询分成两部分,就像@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)发布于 2014-09-27 18:42:56
评论:它只会产生预期的结果,如果电子邮件和电话都是必需的。如果它们是可选的,请让我知道,我会更新我的答案。而且,你不需要为此运行两个查询。谢谢。如果不起作用,请让我知道。
$contdupcheck = "select * from `contact` where `email` = '$email' OR `phone` = '$phone'";将上面的行更改为此
$contdupcheck = "select * from contact where email = '$email' AND phone = '$phone'";https://stackoverflow.com/questions/25946617
复制相似问题