在其他php文件中使用URL变量

时间:2013-05-23 14:29:16

标签: php

我正在使用传递给另一个php文件的URL值。但是,每次我尝试使用name变量的值与下面的普通变量结合时,PHP都不会读取它!

global $counterforlist;
 $counterforlist= 0;
echo "<form method=\"POST\"  action=\" ".htmlspecialchars($_SERVER['PHP_SELF'])."\" > ";
echo "<table>";
echo "<tr><td>ID:</td><td>Company Name</td><td>Date for Service</td><td>Edit</td><td>Delete</td></tr>";

while ($row = mysql_fetch_array($result)){
$counterforlist = $counterforlist +1;
echo "<td><input type= 'text' name = 'jobrequestnumber$counterforlist' value =".$row['jobrequestnumber']."></td>"   ; 
echo "<td><input type= 'text' name = 'requestingcompany$counterforlist' value =".$row['requestingcompany']."></td>" ;
echo "<td><input type= 'text' name = 'dateforService$counterforlist' value =".$row['dateforService']."></td>"   ;
echo "<td><a href=\"update_request.php?jobrequestnumber{$counterforlist}={$row['jobrequestnumber']}&requestingcompany{$counterforlist}={$row['requestingcompany']}&dateforService{$counterforlist}={$row['dateforService']}&{$counterforlist}={$counterforlist}\">Update</a></td>";
echo "<td><a href='delete.php?jobrequestnumber=".$row['jobrequestnumber']."'>Delete</a></td>"; //too
echo "</tr>";
<?php include('update_request.php');?> 

这是调用这些值的另一个php文件 在update_request中:

<?php 
global $counterforlist;

$jobrequestnumber=$_GET["jobrequestnumber"."$counterforlist"];
$requestingcompany=$_GET["requestingcompany"."$counterforlist"];
$dateforService=$_GET["dateforService"."$counterforlist"]; 

$required_array=array($jobrequestnumber,$requestingcompany,$dateforService);
$errors = array();
$errors = array_merge($errors, check_required_fields($required_array, $_POST));
if (empty($errors)){
// Database submission only proceeds if there were NO errors.
    $query =    "UPDATE jobrequest SET 
                        requestingcompany = '{$requestingcompany}',
                        dateforService = {$dateforService} 
                    WHERE jobrequestnumber ={$jobrequestnumber}";
                    echo $jobrequestnumber;
        $result = mysql_query($query);

错误讯息:

  

注意:未定义的索引:jobrequestnumber in   第7行的C:\ xampp \ htdocs \ xampp \ capstone \ update_request.php

     

注意:未定义的索引:在...中请求公司   第8行的C:\ xampp \ htdocs \ xampp \ capstone \ update_request.php

     

注意:未定义的索引:dateforService in   第9行的C:\ xampp \ htdocs \ xampp \ capstone \ update_request.php


如果我再次声明变量,它将不会从以前的php页面读取值。 你能帮我弄清楚我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

是的,是吗?您的其他PHP文件没有定义$counterforlist,所以类似

$jobrequestnumber=$_GET["jobrequestnumber"."$counterforlist"];

被视为

$jobrequestnumber=$_GET["jobrequestnumber"];

因为您要发送jobrequestnumber1jobrequestnumber2等等,所以不会存在简单的jobrequestnumber

PHP中的

global不会使变量在多个DIFFERENT脚本中保持不变。除非你在function()定义中使用该声明,否则反正意义不大。

你可能想要这样的东西:

$jobrequests = preg_grep('/^jobrequestnumber\d+$/', array_keys($_GET)); // get matching key names
foreach($jobrequests as $jobrequest) {
   $key = substr($jobrequest, 12); // extract the digit(s) from the end of the key name
   ...
}