On 10-Jun-2003 Jean-Christian Imbeault wrote:
> [reply to a personal email posted here for the benefit of all :)]
>
<snip>
> > This bugs me because my db has 125 fields and it will be a very long
> sql string!
>
> I bet!
>
> > The form page generates form contents by using a while loop.
> >
> > How would you build the sql string from the form page?
>
> Use a while loop ;) Name the GET or POST vars the same as the field
> names in the DB. Then you could use something like (I say like b/c this
> won't work, it's just an idea):
>
> $sql = "update table A SET ";
> while (list($fieldName, $value) == each($_POST)) {
> $sql .= " $fieldName='$value', ";
> }
>
> This won't work because there will be POST values passes in that are not
> part of your form data. Oh, and there will be a trailing "," you need to
> trim off ...
>
> Just a quick idea.
You can make it a little smarter:
//refetch the old row ...
$qry="SELECT * FROM tbl WHERE id=" .$_POST['id'];
$r=mysql_query($qry);
$row=mysql_fetch_array($r);
unset($chgflds);
foreach($row as $fld => $val) {
if (isset($_POST[$fld]) && ($_POST[$fld] != $val)) {
$chgflds[] = "$fld='" .$_POST[$fld] ."'";
}
}
$update='UPDATE tbl SET ' .implode(', ', $chgflds)
.'WHERE id=' .$_POST['id'];
mysql_query($update);
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php