On 18-Jun-01 WeAreUs wrote:
> Hi All!
> 
> While this is not a huge problem, it is a recurring one so I thought I would
> see if someone else has a nice generic way of handling this situation.   I
> often set up database tables with a PHP4 interface to handle insertions.
> It is always set up on a CGI basis, wherein a form blasts variable names and
> values to the interface, which does the insert.  Often a table consists of a
> few hundred variables.   Clearly when entering the variables by name into
> the statements below, there is room for little errors to creep in, even
> though I copy the var names from a table export.    For example, in a list
> of 500 variable names I might accidentally not copy a variable or miss part
> of one inadvertantly.   This takes a while to find.
> 
> $Entry=MYSQL_QUERY("INSERT INTO seniors(interviewer, surveynum, dat, q1_1,
> q1_2, q1_3, q1_4)
>                VALUES('$interviewer', '$surveynum', '$dat', '$q1_1',
> '$q1_2', '$q1_3', '$q1_4')");
> 
> Being a klutz, I frequently make assorted little errors and it takes some
> time to figure where I have screwed up each and every time I set up one of
> these things.
> 
> I'm wondering therefore if there is a Better Way than using the exact
> variable names.  For example, using something like a while statement
> including :
> while(list($key, $val) = each($HTTP_GET_VARS)) {  clever stuff here }   Does
> anyone have ideas on how to do this better?  I don't think that I am being
> efficient. . .
> 

Exactly right.
  You modify the table, + add the boxes to the form and you're done.

    // get current stuff 
    $qry= "SELECT * FROM webmaster WHERE wmid=$wid";
    $dbres = mysql_query($qry);
    $row = mysql_fetch_object($dbres);
    while (list ($k, $v) = each($row)) {
        if (is_string($k))
            $wm[strtolower($k)]=stripslashes($v);
    }

    if ($Action == 'Update') {
          while (list ($k, $v) = each($HTTP_POST_VARS)) {
            if (isset($wm[$k])) {
                if (0 != strcmp($v, $wm[$k] )) {
                  $qry=sprintf("UPDATE webmaster SET %s='%s' WHERE wmid=%d",
                    $k, $v, $wid); 
                  mysql_query($qry);
               }
           }
        }
 
    } else {    // Display form
         // do the form ...
         echo "<INPUT TYPE=HIDDEN NAME=wid VALUE=$wid";
    }

Regards,
-- 
Don Read                                       [EMAIL PROTECTED]
-- It's always darkest before the dawn. So if you are going to 
   steal the neighbor's newspaper, that's the time to do it.

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to