Jump to content

Gunny

New Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by Gunny

  1. @Gunny:

     

    You have a route_id set in your URL, correct? It should look like: edit.php?route_id=[number]. Route_id should be numeric -- only consisting of numbers -- otherwise you will get an error.

     

    Does that help get you started?

     

     

    Hi Ben,

     

    Yes my url ends like this editroute.php?route_id=13

     

    Thanks for your help, I have been going over and over the code for hours now :bash: but still cannot find where the issue is!

     

    Cheers

     

    Gunny

  2. Hi Ben,

     

    Great example here, I have been mulling over a way to do this for days!

    I have obviously had to amend your code somewhat to fit in with my Database setup and required data. The edit.php is successfully pulling the data required.

    However when I edit the data and click on submit I am getting the Error displayed. I have amended the error messages to identify where the error is, but cannot locate the rror in the code. The error being thrown is for the id being invalid. Can you see where I have gone wrong in the below code at all? Many Thanks Gunny

     

    <?php
    /* 
    EDIT.PHP
    Allows user to edit specific entry in database
    */
    
    // creates the edit record form
    // since this form is used multiple times in this file, I have made it a function that is easily reusable
    function renderForm($id, $routenumber, $depicao, $arricao, $aircrafttype, $error)
    {
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Edit Record</title>
    </head>
    <body>
    <?php 
    // if there are any errors, display them
    if ($error != '')
    {
    echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
    }
    ?> 
    
    <form action="" method="post">
    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
    <div>
    <p><strong>ID:</strong> <?php echo $id; ?></p>
    <strong>Route Number *</strong> <input type="text" name="route_number" value="<?php echo $routenumber; ?>"/><br/>
    <strong>Dep ICAO</strong> <input type="text" size="5" name="dep_icao" value="<?php echo $depicao; ?>"/><br/>
     <strong>Arr ICAO</strong> <input type="text" size="5" name="arr_icao" value="<?php echo $arricao; ?>"/><br/>
      <strong>Aircraft Type*</strong> <input type="text" name="aircraft_type" value="<?php echo $aircrafttype; ?>"/><br/>
    <p>* Required</p>
    <input type="submit" name="submit" value="Submit">
    </div>
    </form> 
    </body>
    </html> 
    <?php
    }
    
    
    
       // connect to the database
           require_once 'connect.php';
    $db_server = mysql_connect($db_hostname, $db_username, $db_password);
    
    if (!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
    
    mysql_select_db($db_database) or die("Unable to Select database: " . mysql_error());
    
    
    // check if the form has been submitted. If it has, process the form and save it to the database
    if (isset($_POST['submit']))
    { 
    // confirm that the 'route_id' value is a valid integer before getting the form data
    if (is_numeric($_POST['route_id']))
    {
    // get form data, making sure it is valid
    $id = $_POST['route_id'];
    $routenumber = mysql_real_escape_string(htmlspecialchars($_POST['route_number']));
    $depicao = mysql_real_escape_string(htmlspecialchars($_POST['dep_icao']));
    $arricao = mysql_real_escape_string(htmlspecialchars($_POST['arr_icao']));
    $aircrafttype = mysql_real_escape_string(htmlspecialchars($_POST['aircraft_type']));
    
    // check that firstname/lastname fields are both filled in
    if ($routenumber == '' || $depicao == '')
    {
    // generate error message
    $error = 'ERROR: Please fill in all required fields!';
    
    //error, display form
    renderForm($id, $routenumber, $depicao, $arricao, $aircrafttype, $error);
    }
    else
    {
    // save the data to the database
    mysql_query("UPDATE route_info SET route_number='$routenumber', dep_icao='$depicao', arr_icao='$arricao', aircraft_type='$aircrafttype' WHERE route_id='$id'")
    or die(mysql_error()); 
    
    // once saved, redirect back to the view page
    header("Location: va.php"); 
    }
    }
    else
    {
    // if the 'route_id' isn't valid, display an error
    echo 'Error Invalid Route ID!';
    }
    }
    else
    // if the form hasn't been submitted, get the data from the db and display the form
    {
    
    // get the 'route_id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
    if (isset($_GET['route_id']) && is_numeric($_GET['route_id']) && $_GET['route_id'] > 0)
    {
    // query db
    $id = $_GET['route_id'];
    $query = "Select * from route_info where route_id=$id";
             $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    
    // check that the 'route_id' matches up with a row in the databse
    if($row)
    {
    
    // get data from db
    $routenumber = $row['route_number'];
    $depicao = $row['dep_icao'];
    $arricao = $row['arr_icao'];
    $aircrafttype = $row['aircraft_type'];
    
    // show form
    renderForm($id, $routenumber, $depicao, $arricao, $aircrafttype, '');
    }
    else
    // if no match, display result
    {
    echo "No results!";
    }
    }
    else
    // if the 'route_id' in the URL isn't valid, or if there is no 'route_id' value, display an error
    {
    echo 'Error!';
    }
    }
    ?>

×
×
  • Create New...