0
我拼凑从各种教程,范例等下面的脚本...PHP表 - 编辑 - 通过文本文件删除数据块
眼下剧本目前保存ID,姓名,地址用“|”分隔符到一个文本文件DB这样的:
1|John|http://www.john.com|
2|Mark|http://www.mark.com|
3|Fred|http://www.fred.com|
但我有一个很难试图使“更新”和“删除”按钮正常工作。
有人可以请张贴代码将:
- 让我更新/保存任何该行更改数据(更新按钮)
- 让我删除该行(用于DELETE键)
请复制n粘贴下面的代码,并为自己尝试。 我想保留以下脚本的输出格式。
<?php
$file = "data.txt";
$name = $_POST['name'];
$url = $_POST['url'];
$data = file('data.txt');
$i = 1;
foreach ($data as $line) {
$line = explode('|', $line);
$i++;
}
if (isset($_POST['submits'])) {
$fp = fopen($file, "a+");
fwrite($fp, $i."|".$name."|".$url."|\n");
fclose($fp);
}
?>
<html>
<head>
</head>
<body>
<br>
<form name="form1" action="thispage.php" method="POST">
<input type="text" name="name">
<input type="text" name="url">
<input type="submit" name="submits" value="ADD"><br>
</form>
<form name="form2" action="thispage.php" method="POST">
<?php
$display = file("data.txt");
for ($i=0; $i<=count($display)-1; $i++) {
$lines = explode("|",$display[$i]);
print('<input type="hidden" name="id" value="'.$lines[0].'">
<input type="text" name="name" value="'.$lines[1].'">
<input type="text" name="url" value="'.$lines[2].'">
<input type="submit" name="update" value="UPDATE">
<input type="submit" name="delete" value="DELETE"><br>');
}
?>
</form>
</body>
</html>