PHP Source Code: fedit.php
<?php
//----------------------------------------------------------
// The Web Language Project
// Mark Brautigam
// May-June 2015
// http://www.mixed-up.com/markb/
//----------------------------------------------------------
// Get the ID from GET or POST and try to find that line
// number in the text file.
$id = getid();
if ($id < 0) {
header ("Location: fread.php");
exit();
}
$src = file ("../data/courses.txt");
$n = count ($src);
if (!is_numeric($id) || $id < 0 || $id > $n-1) {
header ("Location: fread.php");
exit();
}
$line = trim ($src[$id]);
$data = explode ("|", $line);
include "common.php";
headers1();
headers2();
echo sidebar("php", "fedit", "php");
?>
<div id="content">
<h2>PHP: Edit the data file</h2>
<?php
draw_form ($id, $data);
//-------------------------------------------------------------
// The main PHP code is here.
//-------------------------------------------------------------
if (isset ($_POST['submit']) && formHasData($_POST)) {
// write the new data to a temp file
//
$fp = fopen ("../data/courses.tmp", "w");
for ($i=0; $i<$n; $i++) {
if ($i == $id) {
$oldline = $src[$i];
fwrite ($fp, "{$_POST['college']}|{$_POST['dept']}|{$_POST['course']}|{$_POST['desc']}\n");
}
else {
fwrite ($fp, $src[$i]);
}
}
fclose ($fp);
// rename the real file to bak, and the temp file to the real file
//
rename ("../data/courses.txt", "../data/courses.bak");
rename ("../data/courses.tmp", "../data/courses.txt");
$olddata = explode ("|", $oldline);
draw_feedback ($olddata, $_POST);
}
?>
<p><a href='fread.php'>Show the contents of this file »</a></p>
<p><a href='source.php?f=4'>Show PHP source code »</a>
</div>
<?php
footers();
//-------------------------------------------------------------
function draw_form ($id, $data)
//-------------------------------------------------------------
{ ?>
<form name='coursesform' action='fedit.php' method='POST'>
<fieldset>
<legend>Edit a course</legend>
<table>
<tr><td>College:</td><td><input type='text' name='college' value='<?php echo $data[0] ?>' /></td></tr>
<tr><td>Department:</td><td><input type='text' name='dept' value='<?php echo $data[1] ?>' /></td></tr>
<tr><td>Course #:</td><td><input type='text' name='course' value='<?php echo $data[2] ?>' /></td></tr>
<tr><td>Course name:</td><td><input type='text' name='desc' value='<?php echo $data[3] ?>' /></td></tr>
<input type='hidden' name='id' value='<?php echo $id ?>' />
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
</table>
</fieldset>
</form>
<?php
}
//-------------------------------------------------------------
function draw_feedback ($olddata, $post)
//-------------------------------------------------------------
{
// Draw the table that shows the old and new data values
//
echo "<p>The following data was edited in the data file: </p>\n";
echo "<table class='results'>\n";
echo " <tr><th>Field</th><th>Old data</th><th>New data</th></tr>\n";
echo " <tr><td>College</td><td>{$olddata[0]}</td><td>{$post['college']}</td></tr>\n";
echo " <tr><td>Department</td><td>{$olddata[1]}</td><td>{$post['dept']}</td></tr>\n";
echo " <tr><td>Course #</td><td>{$olddata[2]}</td><td>{$post['course']}</td></tr>\n";
echo " <tr><td>Course name</td><td>{$olddata[3]}</td><td>{$post['desc']}</td></tr>\n";
echo "</table>\n";
}
//-------------------------------------------------------------
function formHasData ($p)
//-------------------------------------------------------------
{
$formFields = array ( 'college', 'dept', 'course', 'desc' );
for ($i=0, $n=count($formFields); $i<$n; $i++) {
if (isset($p[$formFields[$i]]) && trim($p[$formFields[$i]]) != "")
return true;
}
return false;
}
//-------------------------------------------------------------
function getid ()
//-------------------------------------------------------------
{
if (!isset ($_GET['id']) && !isset($_POST['id'])) {
// header ("Location: fread.php");
// exit();
return -1;
}
// get the current data from the file
if (isset ($_GET['id']))
$id = $_GET['id'];
else
$id = $_POST['id'];
return $id;
}
?>