Re: How to do a dynamic UPDATE SET
| From: | Russ Michell | Date: | Thu, 01 Jan 1970 00:00:00 +0000 |
| Subject: | Re: How to do a dynamic UPDATE SET | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-12961@lists.php.net to get a copy of this message | ||
Hmmm this a good question, one I've not come across before - so my help may only be of limited
value!
You would have to do an initial query in order to get the table names for your proposed
'universal
query' So presuming you have some method (a variable) of identifying which table you want to
query:
//Get table col names
$sql = "SELECT * FROM $table";
$result = mysql_query($sql,$connect) or die(mysql_error());
$colName = mysql_fetch_field($result)
//Or use mysql_list_field($db,$table,$result)
//See: http://uk.php.net/manual/en/function.mysql-list-fields.php
for more info
//Do your universal query
$sql = "UPDATE $table SET $colName[01]='$pet', $colName[02]='$name' WHERE
id=$id";
$result = mysql_query($sql);
This is NOT tried and tested but simply to give the example that an initial query may be used to
get the field names, (dependant on a set variable to define which table to query) then your
universal update query can be set dependant upon the values of the field names of the initial query.
good luck!
Russ
On Mon, 1 Oct 2001 15:27:52 -0600 =?iso-8859-1?Q?Ren=E9_Fournier?= <rene.fournier@markada.com>
wrote:
> I'm having a REALLY hard time with something that's probably easy to do (for
> someone :-)...
>
> Normally, to perform an update on a table with data from a submitted form, I
> would use something like:
>
> $sql = "UPDATE $table SET pet='$pet', name='$name' WHERE
> id=$id";
> $result = mysql_query($sql);
>
> And it would work. Of course, if I wanted to update a different table (with
> different columns/fields), I would need to only change the SET part of the
> SQL (since the value for $table is dynamically generated). For example,
>
> $sql = "UPDATE $table SET car='$car', year='$year' WHERE
> id=$id";
> $result = mysql_query($sql);
>
> But here's what I want to do now: I want to use the same two lines of code
> for any possible form data that might be submitted--in other words, I don't
> want to have to create unique $sql/$result lines for each and every table in
> my database. I want this .php code to accept whatever number and type of
> form elements/data submitted, and make the appropropriate SET values.
> Anyone know how I can do that? I actually have tried several things to pass
> the form data strcuture (number and names of columns) over to this php code,
> but haven't been able to get anything working. Can anyone help?? Much
> thanks if you can..
>
> ...Rene
>
> ---
> Rene Fournier
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
#-------------------------------------------------------#
"Believe nothing - consider everything"
Russ Michell
Anglia Polytechnic University Webteam
Room 1C 'The Eastings' East Road, Cambridge
e: r.j.michell@apu.ac.uk
w: www.apu.ac.uk/webteam
www.theruss.com
#-------------------------------------------------------#