PHP-条件语句无法正常工作 [英] PHP - conditional statement not working properly

查看:85
本文介绍了PHP-条件语句无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为移动应用程序使用API​​的PHP和JSON。我试图编写一个注册模块,但是部分条件语句无法按预期工作。

I'm working in PHP and JSON at API for my mobile application. I tried to write a registration module, but a part of my conditional statements don't work as expected.

如果语句1:

if(!isset($_GET['username']) || !isset($_GET['password']) || !isset($_GET['imei']) || !isset($_GET['imie']) || !isset($_GET['nazwisko']) || !isset($_GET['email']) || !isset($_GET['zgoda']) || !isset($_GET['telefon']) || !isset($_GET['zgoda2']) || !isset($_GET['kraj']));
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 }

当缺少参数时,应该给出一个错误,但是

It should give an error, when there is an argument missing, but it is giving an error always.

我的查询:

username = konrad12& password = xxx& imei = 000000000000000& nazwisko = Potter& imie = Ronald& email=xxx@xxx.pl& zgoda = 1& telefon = 000& zgoda2 = 1& kraj =波兰

如果语句2:

 if(strlen($c) != 15 || !validEmail($f) || strlen($g) != 1 || strlen($i) != 1 || wez_id_kraju($j) == 0)
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 } 

当var值不正确时,它应该给出一个错误,但是它给出了

It should give an error, when var values are incorrect, but it is giving an error always.

我的变量:

 $z = mysql_real_escape_string($_GET['username']);
 $b = mysql_real_escape_string($_GET['password']);
 $c = mysql_real_escape_string($_GET['imei']);
 $d = mysql_real_escape_string($_GET['nazwisko']);
 $e = mysql_real_escape_string($_GET['imie']);
 $f = mysql_real_escape_string($_GET['email']);
 $g = mysql_real_escape_string($_GET['zgoda']);
 $h = mysql_real_escape_string($_GET['telefon']);
 $i = mysql_real_escape_string($_GET['zgoda2']);
 $j = mysql_real_escape_string($_GET['kraj']);

如果语句3:

 if($g != 0 or 1 || $i != 0 or 1)
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 } 

当$ g或$ i的值是' t 1或0,但始终会出现错误。

It should give an error, when value of $g or $i isn't 1 or 0, but it is giving an error always.

请帮助我,我尝试了很多事情,但是找不到解决方法

Please help me, I tried a lot of things, but I can't find a solution

@Edit:

我的有效电子邮件功能:

My valid email function :

    function validEmail($email)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) && !$atIndex)
       {
          $isValid = false;
       }
       else
       {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);
          if ($localLen < 1 || $localLen > 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen < 1 || $domainLen > 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if
    (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                     str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/',
                 str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }
          if ($isValid && !(checkdnsrr($domain,"MX") || 
     ↪checkdnsrr($domain,"A")))
          {
             // domain not found in DNS
             $isValid = false;
          }
// I add that text ...
      mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
      mysql_select_db(DB_BASE);
      $q = "SELECT * FROM `system_domeny`";
      $a = mysql_query($q);
      while($wynik = mysql_fetch_array($a))
      {
       if($domena == $wynik[1]) $isValid = false;
      }
// ...
       }
       return $isValid;
    }


推荐答案

在最后一个空格后删除分号if的括号。

remove the semicolon after the last bracket of the if.

第一条语句结束

 || !isset($_GET['kraj']));

我认为第二条语句看起来不错,这是电子邮件验证功能还是其他功能的问题在最后一次检查中。

I think the second statement looks ok, is it an issue with the email validation function or the other function in the last check.

最后一条语句应该类似于

the last statement should be something like

if(($g != 0 && $g != 1) || ($i != 0 && $i != 1))

这篇关于PHP-条件语句无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆