PHP(编辑和删除html文件)

PHP(编辑和删除html文件),php,html,mysql,Php,Html,Mysql,我在学校学到的是使用file\u put\u content()输入和显示数据。但是我不知道如何编辑/更新或删除里面的数据 我的“form.php” 我爱: 输入一些食物名称后,它被发送到文件调用“foodscript.php” 然后,创建了foodhistory.html,并存储了我已经在表单中输入的数据。数据在foodhistory.html中被称为“Sushi” 因此,我的问题是,如何使用带有“删除和编辑”按钮的新表单编辑/更新或删除“寿司”数据?如果你们有更好的想法,你介意告诉我

我在学校学到的是使用file\u put\u content()输入和显示数据。但是我不知道如何编辑/更新或删除里面的数据

我的“form.php”


我爱:
输入一些食物名称后,它被发送到文件调用“foodscript.php”


然后,创建了foodhistory.html,并存储了我已经在表单中输入的数据。数据在foodhistory.html中被称为“Sushi”

因此,我的问题是,如何使用带有“删除和编辑”按钮的新表单编辑/更新或删除“寿司”数据?如果你们有更好的想法,你介意告诉我这个过程或方法吗?我只是个学生


非常感谢。

您尚未为表单定义方法,因此浏览器不知道如何发送它。
将表单的method属性设置为“post”。
如果仍然不起作用,请尝试此代码并告诉我们您得到了什么:

echo$\u POST[“foodname”]

首先,您必须知道要编辑/删除的元素,因此表单必须要求食品名称

有鉴于此,要编辑/删除,您可以执行类似的操作

<?php

$file = 'foodhistory.html';

$oldName = $_POST['oldname'];
$newName = $_POST['newname'];
$action  = $_POST['action']; // delete or edit

// this function reads the file and store every line in an array
$lines = file($file);

$position = array_search($oldName, $lines);

if($position) {
    exit('Food not found.');
}

switch($action) {
    case 'delete':
        unset($lines[$position]);
        break;
    case 'edit':
        $lines[$position] = $newName;
        break;
}

file_put_contents($file, implode(PHP_EOL, $lines)); // overwrite stuff on the file with fresh data

不要将数据存储在HTML中,而是将其存储在数据库中。您可以在那里编辑/删除数据。有了这个问题,你真的应该从一些PHP/MySQL教程开始。@David哦,我的学校还没有教这个阶段,但我想在网上找到它。然后你要找的谷歌搜索词是“PHP MySQL教程”。好的,谢谢David。我不同意唯一的方法是数据库。当然,任何实际的应用程序都会使用它,但如果你只是开始你的第一步,就没有必要把它击倒。这个问题有代码,很清楚。。我不明白为什么我们不应该只是帮助Teik,而不是说“做得不同”。哦,对不起,我错了。实际上我忘了把它放在问题上了。好的,谢谢你,我会用你的代码进行实验,然后再给你回复,看看它是否有效。
<?php
  //This is where my input data is being process to the txt file.
   $name= $_POST['foodname'];

  //This is where my data would be store in this file.
   $file= "foodhistory.html"; 

  //The function is begins to created new file and store my data on it.
  file_put_contents($file, $name . PHP_EOL, FILE_APPEND);

 ?>
<?php

$file = 'foodhistory.html';

$oldName = $_POST['oldname'];
$newName = $_POST['newname'];
$action  = $_POST['action']; // delete or edit

// this function reads the file and store every line in an array
$lines = file($file);

$position = array_search($oldName, $lines);

if($position) {
    exit('Food not found.');
}

switch($action) {
    case 'delete':
        unset($lines[$position]);
        break;
    case 'edit':
        $lines[$position] = $newName;
        break;
}

file_put_contents($file, implode(PHP_EOL, $lines)); // overwrite stuff on the file with fresh data