PHP Source Code: dbedit.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 use it to obtain
// this database entry.
//
$id = getid();
if ($id < 0) {
header ("Location: dbread.php");
exit();
}
$dbhrow = get_current_db_data($id);
if ($dbhrow[1] === 0) {
header ("Location: dbread.php");
exit();
}
$dbh = $dbhrow[0];
$row = $dbhrow[1];
include "common.php";
headers1();
headers2();
echo sidebar("php", "dbedit", "php");
?>
<div id="content">
<h2>PHP: Edit the MySQL Database</h2>
<?php print_form ($row); ?>
<?php
//-------------------------------------------------------------
if (isset ($_POST['submit']) && formHasData($_POST))
//-------------------------------------------------------------
{
print_feedback ($row, $_POST);
// special handling for integers
if (!isset($_POST['lat']) || $_POST['lat'] == "")
$_POST['lat'] = 0;
if (!isset($_POST['lon']) || $_POST['lon'] == "")
$_POST['lon'] = 0;
if (strlen($_POST['state']) > 4)
$_POST['state'] = substr ($_POST['state'], 0, 4);
// database handling: UPDATE
$sql = "UPDATE parks SET site='{$_POST{'site'}}', city='{$_POST{'city'}}', state='{$_POST{'state'}}', " .
"latitude={$_POST{'lat'}}, longitude={$_POST{'lon'}} WHERE ID=$id;";
echo $sql;
if (function_exists('mysqli_query'))
$qresult = $dbh->query ($sql);
else
$qresult = mysql_query ($sql);
}
?>
<p><a href='dbread.php'>Show the contents of this database table »</a></p>
<p><a href='source.php?f=9'>Show PHP source code »</a>
</div>
<?php footers(); ?>
<?php
//-------------------------------------------------------------
function formHasData ($p)
//-------------------------------------------------------------
{
$formFields = array ( 'site', 'city', 'state', 'lat', 'lon' );
for ($i=0, $n=count($formFields); $i<$n; $i++) {
if (isset($p[$formFields[$i]]) && trim($p[$formFields[$i]]) != "")
return true;
}
return false;
}
//-------------------------------------------------------------
function print_form($row)
//-------------------------------------------------------------
{ ?>
<form name='parksform' action='dbedit.php' method='POST'>
<fieldset class='db'>
<legend>Edit a park</legend>
<table>
<tr><td>Park:</td><td><input type='text' name='site' value='<?php echo $row{'site'} ?>' /></td></tr>
<tr><td>City:</td><td><input type='text' name='city' value='<?php echo $row{'city'} ?>' /></td></tr>
<tr><td>State:</td><td><input type='text' name='state' value='<?php echo $row{'state'} ?>' />
<span>(4 characters max)</span></td></tr>
<tr><td>Latitude:</td><td><input type='text' name='lat' value='<?php echo $row{'latitude'} ?>' /></td></tr>
<tr><td>Longitude:</td><td><input type='text' name='lon' value='<?php echo $row{'longitude'} ?>' /></td></tr>
<input type='hidden' name='id' value='<?php echo $row{'ID'} ?>' />
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
</table>
</fieldset>
</form>
<?php
}
//-------------------------------------------------------------
function print_feedback ($row, $post)
//-------------------------------------------------------------
{
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>Park</td><td>{$row{'site'}}</td><td>{$post['site']}</td></tr>\n";
echo " <tr><td>City</td><td>{$row{'city'}}</td><td>{$post['city']}</td></tr>\n";
echo " <tr><td>State</td><td>{$row{'state'}}</td><td>{$post['state']}</td></tr>\n";
echo " <tr><td>Latitude</td><td>{$row{'latitude'}}</td><td>{$post['lat']}</td></tr>\n";
echo " <tr><td>Longitude</td><td>{$row{'longitude'}}</td><td>{$post['lon']}</td></tr>\n";
echo "</table>\n";
}
//-------------------------------------------------------------
function get_current_db_data ($id)
//-------------------------------------------------------------
{
// returns two values in an array:
// 1. the dbh for later use
// 2. the current value of data for this $id
// database handling: SELECT
include "./connect.php";
$dbh = my_connect ();
$sql = "SELECT * FROM parks WHERE id='$id'";
// echo "<p>The database query is: $sql</p>\n";
if (function_exists('mysqli_query')) {
$qresult = $dbh->query ($sql);
if ($qresult->num_rows < 1) {
// header ("Location: dbread.php");
// exit();
return 0;
}
$row = $qresult->fetch_assoc();
}
else {
$qresult = mysql_query ($sql);
if (mysql_num_rows($qresult) < 1) {
// header ("Location: dbread.php");
// exit();
return 0;
}
$row = mysql_fetch_assoc($qresult);
}
return array ($dbh, $row);
}
//-------------------------------------------------------------
function getid ()
//-------------------------------------------------------------
{
if (!isset ($_GET['id']) && !isset($_POST['id'])) {
// header ("Location: dbread.php");
// exit();
return -1;
}
if (isset ($_GET['id']))
$id = $_GET['id'];
else
$id = $_POST['id'];
return $id;
}
?>