Page 1 of 1

Edit Details Forms

Posted: Thu Jul 06, 2006 11:00 am
by jamiel
Hi,

Was just wondering what pattern you guys generally use for making "Edit Forms", where basically the form display's a record in the database (Say a users personal details) and then any changes that are made result in an Update to that record in the database. I think my method at the moment is a bit of hack where the id's of my form elements match the column name in the database, and I have a single function which creates my UPDATE statement dynamically using $key => $value .

This method makes input verfication tricky, however this is not too serious as the current system is only going to be used by my boss :)

Anyone have any better ways that don't involve using a framework, model or creating a massive class with setEmail(), setName() functions?

Jamie

Posted: Wed Jul 12, 2006 12:40 am
by Benjamin
Well post some of your code and I'm sure a bunch of us can find things to improve. If you don't want to go oop, you can use something like what I wrote below. With this model you can have multiple actions performed on 1 page, and you can stop processing an action at the first validation failure.

Code: Select all

if (isset($_POST['Action'])) {
  switch($_POST['Action']) {
      case 'Update':
// validation here
          if ($_POST['username'] == '') {
              echo 'Your username cannot be blank';
              break;          
          }

          if ($_POST['password'] == '') {
              echo 'Your password cannot be blank';
              break;          
          }

// if everything is ok we make it here and can update the database...
          break;
    case 'Delete':
// blah
          break;
  }
}