我不知道出了什么问题,但有以下几点。
“如果”条件回声不起作用。谁能告诉我如何解决这个问题?
$select_query = mysql_query(some query);
$fetch_pass = mysql_fetch_array($select_query);
$getpass = $fetch_pass['field_value'];
echo $getpass; //Here it is working
if($_POST['userpass'] == $getpass)
{
echo $getpass; // Here it is working
$select_storename = mysql_query("Some Query");
$fetch_storename = mysql_fetch_array($select_storename);
$getstorename = $fetch_storename['field_value'];
echo $getpass; // Here if $getstorename has output then getpass is working else it is not working
$select_brandname = mysql_query("Some Query"); // This query is never executing
$fetch_brandname = mysql_fetch_array($select_brandname); // This is never working
$getbrandname = $fetch_brandname['field_value']; // This is never working
echo $gettime; // Here it is not working
// None of these if conditions are working.
if($getstorename != null) {
header("location:http://localhost/stores/");
}
if($getbrandname != null) {
header("location:http://localhost/brands/");
}
}
这个概念是$select_query
将获取密码,在第一个“if”条件下我们检查密码是正确还是错误,然后有2个查询$select_storename
和{{1} },只有第一个工作。如果我将订单更改为$select_brandname
,那么只有它有效,第一个查询正在运行,第二个查询无效,“if”条件也不起作用。
更新1
我认为这是由于查询失败,我如何忽略并绕过失败的查询?
答案 0 :(得分:2)
您的代码存在以下问题:
mysql_query()
的返回值。查询可能会失败。mysql_fetch_array()
的返回值。查询可能成功,并返回0行。header()
调用。 (依靠输出缓冲不是一种强大的设计。)另请注意,不推荐使用mysql扩展(并且可能未维护)。您应该使用mysqli或pdo。
您的代码应该像这样 :
$select_query = mysql_query(some query);
if (!$select_query) {
/* Query failed. Display error page. */
exit;
}
$fetch_pass = mysql_fetch_array($select_query);
if (!count ($fetch_pass)) {
/* Password incorrect. Display error page. */
exit;
}
$getpass = $fetch_pass['field_value'];
mysql_free_result ($fetch_pass);
if($_POST['userpass'] !== $getpass) {
/* Password incorrect. Display error page. */
exit;
}
$getstorename = null;
$getbrandname = null;
$select_storename = mysql_query ("Some Query");
if ($select_storename) {
$fetch_storename = mysql_fetch_array ($select_storename);
if (count ($fetch_storename)) {
$getstorename = $fetch_storename['field_value'];
}
mysql_free_result ($select_storename);
}
$select_brandname = mysql_query ("Some Query");
if ($select_brandname) {
$fetch_brandname = mysql_fetch_array ($select_brandname);
if (count ($fetch_brandname)) {
$getbrandname = $fetch_brandname['field_value'];
}
mysql_free_result ($select_brandname);
}
if ($getstorename != null) {
header("location:http://localhost/stores/");
} else if ($getbrandname != null) {
header ("location:http://localhost/brands/");
} else {
/* None found. Display error page or some default page. */
}
关于代码的一些注释:
if...else
梯子。require()
。mysql_free_result()
,即使之后PHP会被清除。如果不出意外,它往往鼓励您编写更清晰的代码。password_hash()
或类似密码加密密码,并仅存储和比较加密密码。有关详细信息,请参阅Password FAQ。