我有两个文件,一个是 list.php ,另一个是 record.php 。通过第一个文件,我可以添加新记录并将其保存到文本文件中。我想要的是当我点击一个对应于特定记录行的按钮时,该记录应该被删除或根据文本文件中选择的选项进行编辑。 list.php 用于显示记录。这是我的代码。 的 list.php的
$choice = array();
$dataCounter = file("data.txt");
$id = 1;
foreach ($dataCounter as $line)
{
$line = explode('|', $line);
$id++;
}
if ($choice == "addRecord")
{
$fileName = "data.txt";
$data = fopen($fileName, "a");
fwrite($data, "$id\t $name\t $phone\t $email\t $address\n");
fclose($data);
}
//function to display records
function viewRecords()
{
?>
<table border="1" cellpadding="5" cellspacing="2" width="300">
<?php
$theData = file("data.txt");
foreach ($theData as $key => $lines)
{
echo("<tr>");
list($id, $name, $phone, $email, $address) = split("\t",$lines);
echo("<td>");
echo $name;
echo $phone;
echo $email;
echo $address;
echo("</td>"); ?>
<td>
<form action="record.php" method="post" name="viewForm" id="viewForm">
<input type="hidden" name="userid" value= "<?php echo $id; ?>"/>
<input type="hidden" name="username" value= "<?php echo $name; ?>"/>
<input type="hidden" name="userphone" value= "<?php echo $phone; ?>"/>
<input type="hidden" name="useremail" value= "<?php echo $email; ?>"/>
<input type="hidden" name="useraddress" value= "<?php echo $address; ?>"/>
<input type="hidden1" name="editId" value= "<?php echo $key; ?>" />
<input type="submit" id="action" name="action" value="Edit"/>
</form>
<input type="submit" id="delete" name="action" value="Delete" onclick="return confirm('Do you want to delete?');"/>
</td>
<?php echo("</tr>");
}
?>
</table>
<?php
}
?>
<html>
<head>
<title> View Page </title>
</head>
<body>
<div class="header">
<div style="border:1px solid black; padding:15px; width:800px">
<table border="1" cellpadding="5" cellspacing="2" width="300">
<tr>
<td>ID</td>
<td>Name</td>
<td>Phone</td>
<td>Email</td>
<td>Address</td>
</tr>
</table>
</div>
<div class="fileSize">
<div style="border:1px solid black; padding:15px; width:800px">
<?php
if (filesize('data.txt') == 0)
{
echo "NO DATA FOUND";
}
?>
</div>
<div class="viewOptions">
<div style="border:1px solid black; padding:15px; width:800px">
<input type="submit" id="submit" name="submit" value="Add New"/>
<p>[ <a href="<?php echo $_SERVER['PHP_SELF']; ?>?choice=view">View</a> ]</p>
<?php
//Main logic what to do when
if ($choice == "view")
{
viewRecords();
}
?>
</div>
</body>
</html>
**record.php**
<?php
$name = "";
$phone = "";
$email = "";
$address = "";
if (!empty($_POST["username"])) $name = $_POST["username"];
if (!empty($_POST["userphone"])) $phone = $_POST["userphone"];
if (!empty($_POST["useremail"])) $email= $_POST["useremail"];
if (!empty($_POST["useraddress"])) $address = $_POST["useraddress"];
?>
<html>
<head>
<title>User Record Entry</title>
</head>
<body>
<center>
<form action="list.php" method="post" name="recordForm" id="recordForm">
<table style="border:3px solid black;" width="350">
<tr>
<td>Name:</td>
<td><input type="text" name="username" id="username" size="21" style="font-family:Verdana; font-size:16px;" value ="<?php echo $name; ?>"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="userphone" id="userphone" size="21" style="font-family:Verdana; font-size:16px;" value ="<?php echo $phone; ?>"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="useremail" id="useremail" size="21" style="font-family:Verdana; font-size:16px;" value ="<?php echo $email; ?>"/></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea rows="4"columns="28" name="useraddress" id="useraddress"style="font-family:Verdana; font-size:16px;"><?php echo $address;?></textarea></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" id="submit" name="submit" value="Add"/>
<input type="hidden" name="choice" id="choice" value="addRecord"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
enter code here
答案 0 :(得分:0)
<?php
$name = "";
$phone = "";
$action = "";
$editId = "";
if (!empty($_POST["username"])) $name = $_POST["username"];
if (!empty($_POST["userphone"])) $phone = $_POST["userphone"];
if (!empty($_POST["action"])) $action = $_POST["action"];
?>
<html>
<body>
<?php
if ($action == "add")
{
$fileName = "filename.txt";
$data = fopen($fileName, "a");
fwrite($data, $name."|---|".$phone."\r\n");
fclose($data);
}
else if($action == "delete")
{
$deleteId = $_POST['deleteId'];
$readData = file("filename.txt", FILE_IGNORE_NEW_LINES);
$arrOut = array();
foreach ($readData as $key => $val)
{
if ($key != $deleteId) $arrOut[] = $val;
}
$strArr = implode("\n",$arrOut);
$fp = fopen('filename.txt', 'w');
if (count($readData) < 0)
{
fwrite($fp, $strArr."\r\n");
}
else
fwrite($fp, $strArr);
fclose($fp);
}
else if($action == "edit")
{
$editId= $_POST["editId"];
$readData = file("filename.txt", FILE_IGNORE_NEW_LINES);
$readData[$editId] = ($name."|---|".$phone."|---|".$email."|---|".$address);
$writeData = implode("\r\n", $readData);
$fileWrite = fopen('filename.txt', 'w');
fwrite($fileWrite, $writeData."\r\n");
fclose($fileWrite);
}
$fileName = "filename.txt";
$readData = file("filename.txt", FILE_IGNORE_NEW_LINES);
?>
<table border="1" width="50%">
<tr>
<td>Sr. No</td>
<td>Name</td>
<td>Phone No</td>
<td colspan = "2">Action</td>
</tr>
<?php
$cnt = 1;
if (count($readData) > 0)
{
foreach ($readData as $key => $val)
{
list($name, $phone) = array_pad(explode("|---|", $val, 3), 3, null);
?>
<tr>
<td><?=$cnt?></td>
<td><?=$name?></td>
<td><?=$phone?></td>
<td>
<form action="add.php" method="post" name="editForm" id="editForm">
<input type="submit" id="btnEdit" name="btnEdit" value="Edit"/>
<input type="hidden" id="editId" name="editId" value="<?php echo $key; ?>"/>
<input type="hidden" name="action" id="action" value="edit"/>
</form>
<form action="" method="post" name="deleteForm" id="deleteForm">
<input type="submit" id="delete" name="delete" value="Delete" onclick="return confirm('Do you want to delete this record?');"/>
<input type="hidden" id="deleteId" name="deleteId" value="<?php echo $key; ?>"/>
<input type="hidden" name="action" id="action" value="delete"/>
</form>
</td>
</tr>
<?php
$cnt++;
}
}
else
{
echo "No Record Found";
}
?>
<tr>
<td colspan = "7"><p>[ <a href="add.php">Add New Record</a> ]</p></td>
</tr>
</table>
</body>
</html>
块引用 将一个表单另存为 display.php ,将其他表单另存为 add.php 并复制粘贴整个代码
<?php
$action = "";
if (!empty($_POST["action"])) $action = $_POST["action"];
$editId = "";
if ($action == "edit")
{
$editId = $_POST["editId"];
$fileName = "filename.txt";
$readData = file("filename.txt", FILE_IGNORE_NEW_LINES);
list($name, $phone) = array_pad(explode("|---|", $readData[$editId], 3), 3, null);
$action = "edit";
$submit = "Update";
$formTitle = "Edit Record";
}
else
{
$name = "";
$phone = "";
$email = "";
$address = "";
$action = "add";
$submit = "Add";
$formTitle = "Add New Record";
}
?>
<html>
<head>
<title>Records</title>
</head>
<body>
<center>
<form action="display.php" method="post" name="recordForm" id="recordForm">
<label><?=$formTitle?></label>
<table style="border:3px solid black;" width="350">
<tr>
<td>Name:</td>
<td><input type="text" name="username" id="username" size="21" style="font-family:Verdana;
font-size:16px;" value="<?=$name?>"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="userphone" id="userphone" size="21" style="font-family:Verdana;
font-size:16px;" value="<?=$phone?>"/></td>
</tr>
<tr>
<td> </td>
<td>
<input type="hidden" id="editId" name="editId" value="<?=$editId?>"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="<?=$submit?>"/>
<input type="hidden" name="action" id="action" value="<?=$action?>"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>