php - 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);
}
?>
谁能知道是什么问题?如何解决?谢谢
解决方案
您需要在 if 条件之前声明变量 $Fac_ID。
在 if 条件之前添加以下行。
$Fac_ID = null;
或禁用 PHP 通知。
推荐阅读
- javascript - FullCalendar dayRender 未在正确的日期呈现价格
- python - 问题:“模块”对象不可调用
- regex - Watson Assistant 不接受实体值模式
- json - 将多个 JSON 字典列表组合成单个字典
- java - com.sun.mail.smtp.SMTPSendFailedException: 541 5.4.1 内容被内部防火墙阻止
- android - Android在另一个类中访问Webview当前url
- python-3.x - 如何对用户输入列表元素进行排序?
- flutter - 如何在同一个颤振应用程序中保留多个方法通道
- android - 为什么我会收到 HMS 感知错误代码 10102?
- python - AttributeError:“历史”对象没有属性“评估”