为什么我的 PHP MySQL 验证代码停止工作?

所以,我有代码可以提交查询并检查它是否有效并基于此返回一条消息。几个月前它曾经工作过。最近,我换了主机(从Windows VPS 到Linux)。

实际的 MySQL 查询本身有效。它成功进入数据库,但代码没有检查它是否成功。

if (Mysql_Update($sql)) // Send the query and check for completion.
{
    $success = "You have updated the records for <b>" . $firstName . " " . $lastName . "</b>";
    renderForm($id,$firstName,$lastName,$number,$email,NULL,$success);
}
else
{   // If the MySql Query fails,throw an error.
    echo danger("The MySql query has failed!");
    echo danger(mysqli_error($conn));
}

这是Mysql_Update

的细分
function Mysql_Query($query)
{
  global $conn; // Utilize the database connection.
  $query = mysqli_query($conn,$query) or die($conn->error);
  return $query;
}

function Mysql_Update($query)
{
  switch (isAdmin())
  {
    case true:
      Mysql_Query($query);
    break;

    default:
    ob_end_clean();
    die('You are not authorized to be in this area.');
    break;
  }
}

正如我之前所说,查询本身经过并更新数据库。但是我的网页正在返回错误消息,对于 mysql_error($conn),它只是一个空的红色框(danger() 创建了红色框)。在切换到新的 VPS 之前,它曾经工作得非常好,这个确切的代码。旧的 DB 配置也使用 root 用户,但新的使用被授予所有 DB 权限的“admin”用户。我不明白为什么这些变化会很重要。没有转移其他设置,例如 php.ini,只有 inetpub。

harry1900 回答:为什么我的 PHP MySQL 验证代码停止工作?

Mysql_update() 不返回值,因此 if (Mysql_Update($sql)) 将始终失败。我看不出这会以任何其他方式奏效。

您需要从 Mysql_Update() 返回一个值。您可以进一步简化它:

function Mysql_Update(string $query):bool
{

  if (isAdmin()) {
      return Mysql_Query($query);
  } else {
    ob_end_clean();
    die('You are not authorized to be in this area.');
  }
}
本文链接:https://www.f2er.com/180.html

大家都在问