-1

Hello I was working on using passed values to the other php file. However every time I tried use the value of name variable conjunction with ordinary variable below, PHP won't read it !

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');?> 

This is the other php file calling those values in 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);

error messages

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


If I were to declare variables again, It won't read values from previous php page. Can you help me figure out what I have been missing :)

4

2 回答 2

1

It seems that you don't declare the counterfor

Why dont you do a

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>"   ;

Then in your 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
}

Now you don't need the counter variable.

or you can do

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

   // here update database
}
于 2013-05-23T14:11:45.800 回答
0

With file 1 you prepare a form containing fields like:

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

When the form is submitted the values of the form-fields are sent back and you can retrieve them from file 2 using $_GET["jobrequestnumberX"], but you need to know 'X'. This practice is kinf of "not right".

What you should do:

  1. Remove $counterforlist from file 1 (all occurences). E.g. write:

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

  2. In file 2 retrieve the variables like this:

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

Please, also, note Brad's comment about the highly insecure nature of your code.

于 2013-05-23T14:12:37.660 回答