检查DB中的值并将其传递给下一页

时间:2010-12-27 05:26:59

标签: php

我有一个字段,如果用户输入数据,它应该去检查db是否存在它应该重定向到下一页。

但在这里我不确定它是否正在检查数据库,但是mysql查询是否正确。

$ThirdPartyCategoryName =$_POST['ThirdPartyCategoryName'];

$activate = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` =  'y' ");

if($activate=='y')
{
    header("location:catact.php");
}
else
{
    //$activate=='NULL';
    header("location:tp_home.php");
}

1 个答案:

答案 0 :(得分:2)

mysql_query返回一个需要使用mysel_fetch_array等函数进行解析的资源。您的第一个条件总是错误的,您必须在比较其中包含的结果之前解析ressource。

$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` =  'y'");
$firstLine = mysql_fetch_array($ressource);

if ($firstLine && $firstLine['delete'] == 'y') {
     // There is an entry with delete = y
} else {
    // There is no entry with delete = y
}

这段代码也可以简化为:

$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` =  'y'");

if (mysql_fetch_array($ressource)) {
     // There is an entry with delete = y
} else {
    // There is no entry with delete = y
}

或者:

$ressource = mysql_query("SELECT * FROM `thirdpartycategorymaster` WHERE `delete` =  'y'");

if (mysql_num_rows($ressource) > 0) {
     // There is an entry with delete = y
} else {
    // There is no entry with delete = y
}