【发布时间】:2016-12-23 19:48:23
【问题描述】:
我有两个文件,一个是 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
【问题讨论】:
标签: php