Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6083

Re: update mysql without manually tying SET statements

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!diablo1.news.osn.de!news.osn.de!diablo2.news.osn.de!news.belwue.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <4224096.JhdAhAFyse@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Wed, 16 May 2012 18:06:57 +0200
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: update mysql without manually tying SET statements
Newsgroups comp.lang.php
References <23804394.335.1337177557295.JavaMail.geo-discussion-forums@ynlm14> <jp0d8i$g9d$1@dont-email.me>
Followup-To comp.lang.php
MIME-Version 1.0
Lines 51
NNTP-Posting-Date 16 May 2012 18:06:57 CEST
NNTP-Posting-Host 9a92439c.newsspool1.arcor-online.net
X-Trace DXC=HPIA5F6R9mHI7\_^6>c20Jic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@gVYO=MmT0fJDZm8W4\YJNLJ`:FmdeL>K@U@3`]Ai;BfBVkPLA65F8IG
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.php:6083

Followups directed to: comp.lang.php

Show key headers only | View raw


Shake wrote:

> El 16/05/2012 16:12, apavluck@gmail.com 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@PointedEars.de>.
 
> 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@94.75.214.39>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

update mysql without manually tying SET statements apavluck@gmail.com - 2012-05-16 07:12 -0700
  Re: update mysql without manually tying SET statements Shake <QUITAESTO@QUITAESTO.NOES> - 2012-05-16 16:23 +0200
    Re: update mysql without manually tying SET statements Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-16 18:06 +0200
    Re: update mysql without manually tying SET statements apavluck@gmail.com - 2012-05-17 11:06 -0700
      Re: update mysql without manually tying SET statements Shake <alex.estevezQUENO@QUENOfilnet.es> - 2012-05-17 21:06 +0200
  Re: update mysql without manually tying SET statements "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-16 22:48 +0200
    Re: update mysql without manually tying SET statements IRC <ircadhikari@gmail.com> - 2012-05-17 04:14 -0700
      Re: update mysql without manually tying SET statements Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-05-18 10:49 +0200
  Re: update mysql without manually tying SET statements bill <nobody@spamcop.net> - 2012-05-17 07:09 -0400

csiph-web