Edit Details Forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Edit Details Forms

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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;
  }
}
Post Reply