<?php

    
// Include the file that holds the database configuration
    
require('./config.php');

    
// Establish Database Connection
    
$db mysql_connect($host,$user,$pass); // Connect to the database server
    
mysql_select_db($database$db);     // Select the database we'll be using
    
    // echo the navigation
    
echo '<a href="index.php">home</a> | <a href="add.php">add data</a> | <a href="retr.php">retreive data</a> | <a href="modify.php"><strong>modify data</strong></a> | <a href="del.php">delete data</a>';
    
    
// Data is being submitted
    
if ($_POST['submit_data']) { // submit_data is set so the form was submitted.. 
        
$id $_POST['id']; // Put the info from the data form field into a single variable for easy access.
        
$data $_POST['data']; // Put the info from the data form field into a single variable for easy access.
        
mysql_query("UPDATE data_table SET data='$data' WHERE id='$id'"); // run the mysql query to update the selected database table row
        
echo '<br/><br/>Data has been modified<br/>'// echo message of succession
    
}
    
    
// Data is being edited
    
if (isset($_GET['edit'])) { // the "edit" variable has been called for by the URI (URL)
        
$id $_GET['edit']; // get the id of the item your editing from the URI
        
$query mysql_query("SELECT id,data FROM data_table WHERE id='$id'"); // select specific data from the database
        
$data mysql_fetch_array($query); // put the returned data in a variable
        // echo the edit page
        
echo '
            <form method="POST" action="modify.php">
                <h2>Modify Data</h2>
                <input type="hidden" name="id" value="'
.$data['id'].'"/>
                Data: <input type="text" size="25" name="data" value="'
.$data['data'].'"/> <input type="submit" name="submit_data" value="Submit"/>
            </form>
                '
;
    }
    
    
// echo the default page
    
if (!isset($_GET['edit'])) { // since "edit" has not been set in the URI we'll print out the default page
        
echo '<h2>Collected Data:</h2>';
        echo 
'Select edit to modify a data item<br/><hr/>';
        
$query mysql_query("SELECT id,data FROM data_table"); // query the database to return all data from the specified table.
        
while ($data mysql_fetch_array($query)) { // put the returned data into an array
            
echo '<b>id:</b> '.$data['id'].'<br/>'// echo the id 
            
echo '<b>data:</b> '.$data['data'].'<br/>'// echo the data
            
echo '<a href="modify.php?edit='.$data['id'].'">edit</a><hr/>'// echo the edit link with the id set as "edit" in the target URI
        
}
    }
    
?>