Results 1 to 2 of 2

Thread: Online Database Management Program

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Online Database Management Program

    Please keep in mind that I come from a .NET windows form background and a front-end (html, css, js, jquery) background.

    I have been commissioned to create an online database management program. I have the website built to display the data, now I need to be able to insert, update, and delete rows in my MySql database. Unfortunately, I have no ideal where to start. PHP's syntax looks like crap and feels like it hasn't evolved since the early millenium.

    So with that being said... what can I use/what are the steps to implement a database management program online?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Online Database Management Program

    you mean like simple CRUD? and using PHP?

    I use fat free framework, and a couple base classes.
    It's pretty trivial to set up a controller and model, then setup the routes for retrieving /saving a form.

    my final model ends up being an empty class.

    my routes look like this
    Code:
    $fw->route('GET /detail/@device_id/',      '\Controller\Device->view');    // view record
    $fw->route('POST /detail/@device_id/',      '\Controller\Device->save');    // save record
    the controller looks like this

    Code:
    <?php
    namespace Controller;
    class Device
    {
        /* validates if a device id exists in the database */
        private function validate_id($fw, $id, $allow_new = true)
        {
            if ($id === 'new' && $allow_new) return true;   // new record
            if (!is_numeric($id)) return false;             // not numeric
            if ($id > 0x7fffffff) return false;             // prevent overflow from getting to SQL Server
            return (new \Model\Device)->count(['device_id=?', $id]);
        }
    
        /* display a record */
        public function view($fw, $params)
        {
            $app = \App::instance();
            $device_id = $params['device_id'];
    
            if (!$this->validate_id($fw, $device_id))
               $fw->error(404);
    
            // load record, copy to @ROW
            if ($device_id == 'new') $device_id = null;
            $device = new \Model\Device($device_id);
            $device->copyto('ROW');
    
            // load preserved inputs if they exist
            if ($fw->exists('SESSION.record'))
            {
                $fw->ROW = array_merge($fw->ROW, $fw->get('SESSION.record'));
                $fw->clear('SESSION.record');
            }
    
            $app->render('header');
            $app->render('detail');
            $app->render('footer');
        }
    
        public function save($fw, $params)
        {
            $app = \App::instance();
            $device_id = $params['device_id'];
            if (!$this->validate_id($fw, $device_id))
               $fw->error(404);
    
            // start a new record, or load existing
            $device_id = $device_id == 'new' ? null : $device_id;
            $device = new \Model\Device($device_id);
    
            // validate input
            // TODO: use a validation class
            $parse_errors = [];
            foreach ($fw->POST as $field => $value)
            {
                  // Do Validation here
            }
    
            if (count($parse_errors))   // validation errors!
            {
                // Setup error message 
            }
            else
            {
                // allows correction of records before ID is issued
                // locks down edit after issue of device number
                $fw->clear('POST.device_number');   // block device_number from being updated
                $device->copyfrom('POST');          // copy POSTed parameters into our record
                $error = false;
    
                // enable exceptions for this transaction
                $fw->db->setattribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                try
                {
                    $device->save();
                    $device_id = $device->device_id;
                }
                catch (\PDOException $e)
                {
                    $error = $e->errorInfo;
                }
    
                if ($error)
                {
                }
            }
    
            if (!$device_id)
                $fw->reroute("/detail/new");
            else
                $fw->reroute("/detail/$device_id");
        }
    }
    the form template looks like this
    Code:
                <form method="POST">
                        <div class="form-group">
                            <label for="pole_number">Pole / Structure # <span style="color:red">*</span></label>
                            <input id="pole_number" name="pole_number" type="text" class="form-control" value="{{@ROW.pole_number}}" maxlength="64">
                        </div>
                        <button type="submit" class="btn btn-primary"><i class="fa fa-floppy-o"></i> Save</button>
                </form>


    edit: I recommend https://fatfreeframework.com/3.6/home
    simply based on the fact that it's a micro framework that you can read in its entirety in less than a day. The design is simplistic enough to easily extend, and fix any bugs you find in it. the team that manages it is pretty responsive too. It also doesn't box you into any conventions, which are impossibly narrow in other frameworks.
    Last edited by DEXWERX; Sep 15th, 2017 at 08:15 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width