Re: update mysql without manually tying SET statements



Shake wrote:

El 16/05/2012 16:12, apavluck@xxxxxxxxx escribió:
I am trying to setup some generic code that will allow me to update a
mysql database via form and php. I can capture the data in the row of
the database that I want to update. I can present that in a form
populated with the existing values. What I can't figure out how to do is
to avoid typing out the SET statements in the update query.
[...]
The reason I want to do this is because I manage many projects all with
their own variables and databases and the update is the only part that I
have to type in manually.

Withouth entering in details, security and best practices...

$sqlString = ' UPDATE blablalba SET ';

The leading space does not make sense.

foreach($_POST as $key => $value)
{
$sqlString .= " `$key` = '$value', ";
}

// You have to deal here a little with the last comma ;)

That is one reason why you should _not_ build queries that way, but at least
join an array instead. See also <news:1762069.ZWGnKmheAe@xxxxxxxxxxxxxx>.

It's possible not all data in $_POST are fields of the database. You
could manage prefixing the indexes or other ways...

foreach($_POST as $key => $value)
{
if(!preg_match('/^my_prefix.+/',$key)) continue;
$sqlString .= " `$key` = '$value', ";
}

Or you filter out the items that you do not want, by key, using the
array_filter() function, which also returns the array to join that
I mentioned above.

<http://php.net/array_filter>


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@xxxxxxxxxxxx>
.



Relevant Pages

  • compiler and metadata, request opinions...
    ... a lot of the upper/middle compiler machinery is still lacking (such as ... embed the metadata directly into the object modules (the reason being that ... request for a particular piece of information is embedded in a symbol (sort ... it will be loaded into an in-memory version of the database. ...
    (comp.compilers)
  • misc: compiler and metadata...
    ... a lot of the upper/middle compiler machinery is still lacking (such as ... embed the metadata directly into the object modules (the reason being that ... request for a particular piece of information is embedded in a symbol (sort ... it will be loaded into an in-memory version of the database. ...
    (comp.lang.misc)
  • RE: how to list LE options
    ... for whatever reason an applications programmer has to look at this ... curiousity and desire to expand their knowledge, there is no real reason ... LE options were copied to an application parmlib, ... There's no reason that Database, CICS and other groups can't maintain ...
    (bit.listserv.ibm-main)
  • RE: [dbi] RE: Accessing MS Access through the DBI ODBC
    ... reason for this issue is the mdb is on a mapped drive where the mapped driver ... > still getting 'Unable to connect to database'. ... > use DBI; ... Then it will simply display it and die since there ...
    (perl.dbi.users)
  • Re: update 500000 records with adodb.recordset resize the mdb from 140M to 2G
    ... What is the reason to make it in two steps? ... why Compact procedure exists in this DBMS. ... changing connection string of Connection object or changing ODBC record ... this database should be replaced. ...
    (microsoft.public.vb.database.ado)