PHP - 更新表单数据。 (在同一页面上)

时间:2016-05-22 10:43:07

标签: php forms csv

在同一页面上按下提交按钮后,我需要能够更新我的表单数据。

我有一个csv文件。目前,我已经研究出如何编辑其中的数据,并将其保存在csv文件中(通过表单)。 - (我已经附上了下面的主要代码,但即使是任何建议也会有所帮助)

表单和php执行在同一页面上,但是一旦用户编辑了值并按下提交,数据就会返回到原始数据。所以它在csv文件中更新,但不在表单中更新。

这是表格:

for ($i=0; $i<200; $i++) {
    if(empty($SearchResults[$i][0]) == false){ //If its full

    ?>
    <form method="post" action="">

  Full Name: <input name = "Name2" type="text" value="<?php echo $SearchResults[$i][0] ?>"/>

  Weight (kg): <input name="Weight2" type="number" value="<?php echo $SearchResults[$i][1] ?>"/>

 Weight of belongings (kg): <input name="WeightOfBelongings2" type="number"value="<?php echo $SearchResults[$i][2] ?>"/>

  <input name="submit" type="submit" />
  </form>

    <?php

        $i++;

    }else if (empty($SearchResults[$i][0]) == true){ //If it is empty 

    $i =201;



    }
}

按下提交按钮时会发生这种情况:

if (isset($_POST['submit'])) {


    $FullName = $_POST['Name2'];
    $Weight = $_POST['Weight2'];
    $WeightOfBelongings = $_POST['WeightOfBelongings2'];

//Creates a new felon from the class in felen.php and assigns it to this variable $Felon
$Felen = new felen;

//This refers to a function in the class Felen. From this information it calculates the costs, ammounts etc. And saves it to a variable that can be called.
$Felen -> CreateFelen($FullName, $Weight, $WeightOfBelongings);

//This is a for loop that checks when there is an avaliable line to put the data into.
if ($FullName != "") {
    $i = 0;
for ($i=0; $i<200; $i++) {


    if(empty($csv[$i][0]) == false){ //If its full
    if($csv[$i][0] == $_POST['Name2']){

        //its only printing it if it is in the first row?

        $csv[$i][0] = $FullName;
        $csv[$i][1] = $Weight;
        $csv[$i][2] = $WeightOfBelongings;
        $csv[$i][3] = $Felen->FelenTotalWeight;
        $csv[$i][4] = $Felen->UnassembliumRequired;
        $csv[$i][5] = $Felen->ReassembliumRequired;
        $csv[$i][6] = $Felen->CostOfUnassemblium;
        $csv[$i][7] = $Felen->CostOfReassemblium;
        $csv[$i][8] = $Felen->TotalCostOfJourney;

        $i = 201;


    }
    }

  } 



//Saves the previous data and new changes to the csv file
//This opens to file as a write file
$fp = fopen('data.csv', 'w');


//This takes the array that has had data ddded to it and adds it to the file

foreach ($csv as $fields) {
    fputcsv($fp, $fields);
}

//This closes the file
fclose($fp);


}
}

非常感谢!

0 个答案:

没有答案