检查数据库中是否已存在字符串

时间:2014-08-26 18:13:10

标签: php mysql

我做了一个代码来检查数据库中是否已经存在2个字符串,但它似乎不起作用,它仍然将查询插入数据库。所以我想知道什么是错的,谢谢

这是我的代码:

$check_exits = mysqli_query($db_t,"SELECT * FROM `iw4m_clans` WHERE `name` = '".$clan_name."' OR `tag` = '".$clan_tag."'") or die(mysqli_error($db_t));

if(empty($clan_name) || empty($clan_tag) || empty($clan_type))
{
    header("Location: create.php?result=0");
    return;
}
if(mysqli_num_rows($check_exists) >= 1)
{
    header("Location: create.php?result=2");
    return;
}
else
{

    if($clan_type == "Open clan")
        $clan_type = 1;
    else
        $clan_type = 2;

    mysqli_query($db_t,"INSERT INTO `iw4m_clans` (`id`,`name`,`tag`,`owner`,`members`,`type`) VALUES(DEFAULT,'" . $clan_name . "','" . $clan_tag . "','" . $username . "','none','" . $clan_type . "')") or die(mysqli_error($db_t));
    header("Location: create.php?result=1");
}

1 个答案:

答案 0 :(得分:5)

你有一个错字

$check_exits

中的

$check_exits = mysqli_query...

应为$check_exists = mysqli_query...

然后代替if(mysqli_num_rows($check_exists) >= 1)(如果它不起作用; 建议

请尝试使用if(mysqli_num_rows($check_exists) >0)

另外,而不是return;使用exit;