如何在php中使用名称变量?

时间:2013-05-23 13:56:46

标签: php

您好我正在使用传递给其他php文件的值。但是每次我尝试使用name变量的值与下面的普通变量一起使用时,PHP都不会读取它!

global $counterforlist;
 $counterforlist= 0;
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);

错误消息

Notice: Undefined index: jobrequestnumber in C:\xampp\htdocs\xampp\capstone\update_request.php on line 7

Notice: Undefined index: requestingcompany in C:\xampp\htdocs\xampp\capstone\update_request.php on line 8

Notice: Undefined index: dateforService in C:\xampp\htdocs\xampp\capstone\update_request.php on line 9


如果我再次声明变量,它将不会读取以前php页面中的值。 你能帮我弄清楚我一直缺少的东西:)

2 个答案:

答案 0 :(得分:1)

您似乎没有宣布反击

为什么不做

echo "<td><input type= 'text' name = 'jobrequestnumber[]' value =".$row['jobrequestnumber']."></td>"   ; // results in the same jobrequestnumbers
echo "<td><input type= 'text' name = 'requestingcompany[]' value =".$row['requestingcompany']."></td>" ;//this too
echo "<td><input type= 'text' name = 'dateforService[]' value =".$row['dateforService']."></td>"   ;

然后在你的PHP中 $ size = count($ _ GET [“jobrequestnumber”]);

for($X=0; $Size<$X; $X++){
   $jobrequestnumber=$_GET["jobrequestnumber"][$X];
   $requestingcompany=$_GET["requestingcompany"][$X];
   $dateforService=$_GET["dateforService"][$X];

   // here update database
}

现在你不需要计数器变量。

或者你可以做

foreach ($_GET["jobrequestnumber"] as $key => $value){
   $jobrequestnumber=$value;
   $requestingcompany=$_GET["requestingcompany"][$key];
   $dateforService=$_GET["dateforService"][$key];

   // here update database
}

答案 1 :(得分:0)

使用文件1,您可以准备包含以下字段的表单:

<input type='text' name='jobrequestnumberX'...

当提交表单时,表单字段的值将被发回,您可以使用$ _GET [“jobrequestnumberX”]从文件2中检索它们,但您需要知道'X'。 这种做法是“不对”的结果。

你应该做什么:

  1. 从文件1中删除$counterforlist(所有出现)。例如。写:

    echo "<td><a href=\"update_request.php?jobrequestnumber={$row['jobrequestnumber']}&requestingcompany={$row['requestingcompany']}&dateforService={$row['dateforService']}\">Update</a></td>";

  2. 在文件2中检索如下变量:

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

  3. 另外,请注意Brad关于代码高度不安全性的评论。