PhP获取数据未分配给变量

问题描述:

我正在使用PHP为小型项目生成条形码,但似乎无法使其工作。我是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'];"实际上不拉得的数据!如果我取消注释"//$invoice="36";并注释掉get部分,代码将正常工作。

任何想法?

+0

尝试使用print_r($ _GET)或print_r($ _REQUEST)来查看实际获得的值。我猜猜这里有一个错字。另外,只需查看URL即可轻松检查GET变量。 – 2012-03-28 21:46:23

+0

变量$ _GET在url中? 'test.php的?发票= 45' – tttony 2012-03-28 21:47:24

+0

我可以去Site.com/tmp/barcodegen.1d-php5.v4.1.0/test.html?invoice=33 – NRGdallas 2012-03-28 21:53:15

你实际上传递发票数据的URL查询字符串?这就是$ _GET的用途。如果您从表单提交数据,请改用$ _POST。

如果不帮助,尝试print_r($_GET),并确保数据实际被传递你期望的方式,并且在正确的变量名。

+0

不作为获得或从表单工作后,这两种方法导致一个“错误:没有数据输入” – NRGdallas 2012-03-28 22:03:52

你确定你用GET提交,而不是帖子?尝试做print_r($_GET);print_r($_POST);并查看“发票”的位置。

+0

我可以去Site.com/tmp/barcodegen.1d-php5.v4.1.0正好验证网址/test.html?invoice=33 – NRGdallas 2012-03-28 21:49:02

+0

您是否在本地服务器上运行此操作? – 2012-03-28 22:05:14

+0

不,目前托管在一个全功能的网站上,长期以来一直运行完整的php解决方案(zen cart,interspire),没有任何问题。 – NRGdallas 2012-03-28 22:06:22

试图加入这个到您的代码

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

没了,整个代码停止工作,简单地显示alt标签,它是硬编码的“条码”而不是动态图像 – NRGdallas 2012-03-28 22:04:38

的顶部就意味着你不发送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 
}