【问题标题】:Trying to pass value to ISSET condition but getting error尝试将值传递给 ISSET 条件但出现错误
【发布时间】:2019-11-29 12:24:09
【问题描述】:

我创建了一个 PHP 脚本,如果使用该脚本,它总是会进入 else 状态,我不确定为什么它不会进入 else 状态。

  <?php
   require_once  'db_functions.php';
   $db = new DB_Functions();
   $response = array();

   $phone="1234";
   $name="Test";
   $birthdate="1994-01-01";
   $address="123 M";

    if(isset($_POST['phone']) &&
    isset($_POST['name']) &&
    isset($_POST['birthdate']) &&
    isset($_POST['address']))


   {
    echo "Hello World 1";

    $phone = $_POST['phone'];
    $name = $_POST['name'];
    $birthdate = $_POST['birthdate'];
    $address = $_POST['address'];

    echo "Hello World 2";

   }

   else{

    echo "Hello";
    $response["error_msg"] = "Required parameter 
    (phone,name,birthdate,address) is missing!";
    echo json_encode($response);
    }
    ?>

输出:

_msg":"缺少必填参数(电话、姓名、生日、地址)!"}

如果值被传递,那么它应该进入 if 条件而不是 else 条件。

尝试过的选项

尝试了以下选项,但我得到的是空值:

$test=$_POST['电话']; echo "嘿......".$test;

echo isset($_POST['phone']);

使用的网址 https://www.aaa.ccc/php/register.php?phone=232&name=test&birthdate=1954-04-04&address=232

【问题讨论】:

  • 为了获得更好的想法,请尝试打印每个条件以及$_POST。喜欢echo isset($_POST['phone'])
  • 您在 if 条件下检查 $_POST 但您分配的变量是本地的。你确定你在 $_POST 中得到了什么。
  • 我收到意外 T_ECHO 错误。 if(echo isset($_POST['phone']) && echo isset($_POST['name']) && echo isset($_POST['birthdate']) && echo isset($_POST['address']))
  • 您不需要在 IF 语句中回显,您描述相关代码的方式很好。
  • 尝试使用$_GET[] 而不是$_POST[]

标签: php


【解决方案1】:

您是使用GET 方法传递url 中的参数,而不是POST,所以您需要:

 <?php
   require_once  'db_functions.php';
   $db = new DB_Functions();
   $response = array();

   $phone="1234";
   $name="Test";
   $birthdate="1994-01-01";
   $address="123 M";

    if(isset($_GET['phone']) &&
    isset($_GET['name']) &&
    isset($_GET['birthdate']) &&
    isset($_GET['address']))


   {
    echo "Hello World 1";

    $phone = $_GET['phone'];
    $name = $_GET['name'];
    $birthdate = $_GET['birthdate'];
    $address = $_GET['address'];

    echo "Hello World 2";

   }

   else{

    echo "Hello";
    $response["error_msg"] = "Required parameter 
    (phone,name,birthdate,address) is missing!";
    echo json_encode($response);
    }
    ?>

【讨论】:

  • GET 工作正常,但为什么 Post 在这种情况下不起作用。
  • ARC POST 工作正常,但无法通过网络链接工作
猜你喜欢
  • 1970-01-01
  • 2017-10-31
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
相关资源
最近更新 更多