首页 > 解决方案 > PHP注意:调用API到PHP页面时未定义的变量

问题描述

目前我创建了一个具有添加功能的系统。在添加之前,函数需要检查数据库中是否已经存在数据。

这两个函数需要调用两个 API。现在,当我运行代码时,我得到了这个错误:

PHP 注意:未定义变量:C:\inetpub\wwwroot\ebooking\api\api_add_factory.php 第 5 行中的 Fac_ID

以下是我当前的代码:

add_factory.php

<?php

    include("../../config/check.php");
    include("../../api/api_add_factory.php"); //include web services

    if(isset($_POST['Submit']))
    {

      $Fac_ID = $_POST['Fac_ID'];

      $queryt = callApi1();

      if(empty($queryt)){

              $json2 = callApi2();

              if(!empty($json2)){

                header("Location:factory.php");

              }else{
                echo "
                <script>alert('Something wrong, please try again')</script>
                <script>window.location = 'factory.php'</script>
                ";

              }
            }
      else{

          echo "
          <script>alert('The factory you want to add is already exist')</script>
          <script>window.location = 'factory.php'</script>
          ";

      }
    }

?>

<form action="" method="post">
    <table width="90%">
      <tr>
        <td width="20%"><b>Factory Name</b></td>
        <td width="50%"><input type="text" onkeyup="this.value = this.value.toUpperCase();" class="form-control" name="Fac_ID" required></td>
     </tr>
    </table>
    <br>
    <input type="submit" name="Submit" value="Add" class="btn btn-primary btn-block" onclick="return confirm('Do you want to add this factory?');">
</form>

api_add_factory.php

<?php

    function callApi1(){

        $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/selectFactory?Fac_ID=$Fac_ID"; //line 5
        $data = file_get_contents($url);
        return json_decode($data);
    }

    function callApi2(){

        $url2 = "http://172.20.0.45/TGWebService/TGWebService.asmx/insertFactory?fac_id=$Fac_ID&fac_name=$Fac_ID";
        $data2 = file_get_contents($url2);
        return json_decode($data2);
    }

?>

谁能知道是什么问题?如何解决?谢谢

标签: phpmysql

解决方案


您需要在 if 条件之前声明变量 $Fac_ID。

在 if 条件之前添加以下行。

$Fac_ID = null;

或禁用 PHP 通知。


推荐阅读