PhP获取数据未分配给变量


PhP Get Data Not being assigned to Variable

我正在使用PHP为一个小项目生成条形码,但似乎无法使其工作。我是PHP的初学者,似乎找不到我做错了什么。

这是我的代码:

<?php
// Including all required classes
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
// Including the barcode technology
require_once('class/BCGcode39.barcode.php');
// Loading Font
$font = new BCGFontFile('./class/font/Arial.ttf', 18);
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
//$invoice="36";
$drawException = null;
$invoice = $_GET['invoice'];
try {
$code = new BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse($invoice); // Text
} catch(Exception $exception) {
$drawException = $exception;
}

问题是第22行"$invoice = $_GET['invoice'];"实际上并没有拉取get数据!如果我取消对"//$invoice="36";的注释并注释掉get部分,那么代码就可以正常工作。

有什么想法吗?

您是否真的在URL查询字符串中传递发票数据?这就是$_GET的作用。如果您从表单提交数据,请改用$_POST。

如果这没有帮助,请尝试print_r($_GET),并确保数据确实以您期望的方式传递,并且使用正确的变量名。

您确定使用GET而不是POST提交吗?试着做print_r($_GET);print_r($_POST);,看看"发票"在哪里。

尝试将其添加到代码的顶部

if(isset($_GET['invoice'])){$invoice = $_GET['invoice']; }else{ echo 'No invoice'; exit;}

IT意味着您没有将invoice作为GET方法发送,您需要检查它是否存在。

$invoice = isset($_GET['invoice']) ? $_GET['invoice'] : 'default invoice';
/*I dont know what is invoice var and its type , but its always good to validate 
   user input*/
if(validInvoice($invoie)) {
  //doStuff
}