[thelist] design advice on add/edit pages in php

Howard Cheng howcheng at ix.netcom.com
Fri Nov 15 19:47:00 CST 2002


I don't know about the BEST way, but this is how I always do it, and I
think it works fine.

As you have it, the add/edit pages are one and the same, but I always use a
GET instead of a post. The structure tends to be like:

<?
$isNew = (!$id);

// connection stuff here
$db = mysql_connect("...");

// process form here
if ($action)
{
         switch ($action)
         {
                 case "insert":
                         // sql
                         break;
                 case "update":
                         // sql
                         break;
         }
         $result = mysql_query($sql, $db);
}

if ($isNew)
{
         $action = "insert";
}
else
{
         $action = "update";
         // query db here
         $sql = "SELECT ...";
         $result = mysql_query($sql, $db);
         $myrow = mysql_fetch_array($result);

         $name = $myrow['name'];
         // etc
}
?>
<form action="post" method="<?= $PHP_SELF ?>">
<input type="hidden" name="id" value="<?= $id ?>">

<input type="text" name="name" value="<?= $name ?>">
<!-- etc -->
<input type="submit" name="action" value="<?= $action ?>">
</form>

I definitely prefer the single page for add/edit, thus keeping everything
in one form. And by determining if something is new merely by existence of
the ID means I don't have to specify anything other than that.

At 04:59 PM 11/15/2002 -0800, Tom Dell'Aringa wrote:
>Now I've often done in the past, is submit the ADD to the very same
>page, and use the same page for the edit, just dumped the newly added
>info into it.
>
>I'm also trying to use this same page when coming to edit a field
>from another page using a querystring like edit.php?mode=edit&id=3.
>
>My question is - is this the best way to do things? I could have a
>separate edit page - although that would mean I would have the same
>form twice.

::::::::::::::::::::::::::::::::::
Howard Cheng
http://www.howcheng.com/ <-- NEW!
howcheng at ix dot netcom dot com
AIM: bennyphoebe
ICQ: 47319315




More information about the thelist mailing list