else语句中的Javascript不起作用。消息未出现

时间:2019-03-15 09:49:40

标签: javascript php

我正在为php网站进行基本登录,如果检查使用POST发送的数据是否在MySQL表中以及是否正确,它将使用户继续处理数据不正确,该功能非常简单,它应该显示一条消息,提示凭据不正确:

<?php
   include("required/datos.php");
   session_start();

   if ($_SERVER["REQUEST_METHOD"] == "POST") {

      $mypassword = mysqli_real_escape_string($db,$_POST['password']);
      $grupo = mysqli_real_escape_string($db,$_POST['grupo']); 

      $sql = "SELECT * FROM usuarios WHERE BINARY password = '$mypassword' and grupo = '$grupo'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);


      $count = mysqli_num_rows($result);


      if ($count == 1) {
         $_SESSION['grupo'] = $grupo;
         $_SESSION['autorizado'] = TRUE;
         header('location: horario.php');
      }
   } else {
      $message = "Credenciales incorrectas";
      echo "<script type='text/javascript'>alert('$message');</script>";
      header('Refresh: 0; URL=index.php');
   }
?>

if语句到目前为止一直有效,甚至else语句仍然有效,但该消息根本没有显示出来,我试图查看问题的确切出处,但我找不到它。

4 个答案:

答案 0 :(得分:2)

尝试此代码。您的括号位置错误。

<?php
include("required/datos.php");
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $mypassword = mysqli_real_escape_string($db,$_POST['password']);
   $grupo = mysqli_real_escape_string($db,$_POST['grupo']); 

   $sql = "SELECT * FROM usuarios WHERE BINARY password = '$mypassword' and grupo = '$grupo'";
   $result = mysqli_query($db,$sql);
   $row = mysqli_fetch_array($result,MYSQLI_ASSOC);


   $count = mysqli_num_rows($result);


   if ($count == 1) {
      $_SESSION['grupo'] = $grupo;
      $_SESSION['autorizado'] = TRUE;
      header('location: horario.php');
   } else {
      $message = "Credenciales incorrectas";
      echo "<script type='text/javascript'>alert('$message');</script>";
      header('Refresh: 0; URL=index.php');
   }
}
?>

答案 1 :(得分:0)

然后尝试一下

long int a=10,*p;
p=&a;
&a; // where that data is stored
p; // where that data is stored
a; // value of that data
*p; // value of that data

答案 2 :(得分:0)

尝试这个。可能您有一个额外的括号}或放错了位置,并且您正在将$message作为一个错误的字符串回显。通过这种方式使用串联来打印您的消息

$message = "Credenciales incorrectas";
     echo "<script type='text/javascript'>alert('" . $message . "');</script>";
     header('Refresh: 0; URL=index.php');

答案 3 :(得分:0)

就像注释中提到的那样,您有一个卷曲的表情,弄乱了您的代码。 else语句现在正在检查用于检查请求类型的第一个if语句。但是带计数的else情况应该是if语句中检查行数($ count)的else情况。

此外,您还需要在回显中连接字符串,因为您的JS代码不了解PHP脚本中的变量或值,如下所示:

echo "<script type='text/javascript'>alert('" . $message . "');</script>";