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


Groups > comp.lang.php > #6078 > unrolled thread

update mysql without manually tying SET statements

Started byapavluck@gmail.com
First post2012-05-16 07:12 -0700
Last post2012-05-17 07:09 -0400
Articles 9 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  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

#6078 — update mysql without manually tying SET statements

Fromapavluck@gmail.com
Date2012-05-16 07:12 -0700
Subjectupdate mysql without manually tying SET statements
Message-ID<23804394.335.1337177557295.JavaMail.geo-discussion-forums@ynlm14>
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.

ex

$sql = "update myhugedatabase
SET
reporter = '$_POST[reporter]',
date = '$_POST[date]',
district = '$_POST[district]',
village = '$_POST[village]',
school = '$_POST[school]',
address = '$_POST[address]',
teacher = '$_POST[teacher]',
teacher_contact = '$_POST[teacher_contact]',
alt_teach = '$_POST[alt_teach]',
alt_teach_contact = '$_POST[alt_teach_contact]',
gps = '$_POST[gps]',
etc
etc
etc

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.

[toc] | [next] | [standalone]


#6079

FromShake <QUITAESTO@QUITAESTO.NOES>
Date2012-05-16 16:23 +0200
Message-ID<jp0d8i$g9d$1@dont-email.me>
In reply to#6078
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 ';
foreach($_POST as $key => $value)
{
   $sqlString .= " `$key` = '$value', ";
}

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

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', ";
}

That's some basic ideas, but be carefull, because there are important 
things to take in care when using this metodologies. Using POST data to 
build SQL strings "automagically" implies taking care of sqlInjection 
and thiese kind of things.

Greetings

[toc] | [prev] | [next] | [standalone]


#6083

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-05-16 18:06 +0200
Message-ID<4224096.JhdAhAFyse@PointedEars.de>
In reply to#6079
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>

[toc] | [prev] | [next] | [standalone]


#6094

Fromapavluck@gmail.com
Date2012-05-17 11:06 -0700
Message-ID<28549321.742.1337277977015.JavaMail.geo-discussion-forums@yneh4>
In reply to#6079
Shake's suggestion worked exactly how I needed it to work.  I had to substring some things off as he said, but all in all, was just what was needed.

Thanks!!!!


On Wednesday, May 16, 2012 10:23:12 AM UTC-4, 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 ';
> foreach($_POST as $key => $value)
> {
>    $sqlString .= " `$key` = '$value', ";
> }
> 
> // You have to deal here a little with the last comma ;)
> 
> 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', ";
> }
> 
> That's some basic ideas, but be carefull, because there are important 
> things to take in care when using this metodologies. Using POST data to 
> build SQL strings "automagically" implies taking care of sqlInjection 
> and thiese kind of things.
> 
> Greetings

[toc] | [prev] | [next] | [standalone]


#6095

FromShake <alex.estevezQUENO@QUENOfilnet.es>
Date2012-05-17 21:06 +0200
Message-ID<jp3i8g$nmr$1@dont-email.me>
In reply to#6094
apavluck@gmail.com a écrit :
> Shake's suggestion worked exactly how I needed it to work.  I had to 
> substring some things off as he said, but all in all, was just what was 
> needed.
>
> Thanks!!!!
>

But remeber, I said "Withouth entering in details, security and best 
practices"

This means that I explained "the basic" but there are better 
"aproximations" to resolve this problem.

And some of them had been explained in this thread. The fact is that 
usually are a better way to do the same.

Greetings

[toc] | [prev] | [next] | [standalone]


#6087

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-16 22:48 +0200
Message-ID<a1ii4oFg5dU1@mid.uni-berlin.de>
In reply to#6078
Am 16.05.2012 16:12, schrieb apavluck@gmail.com:
> 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.
> 
> ex
> 
> $sql = "update myhugedatabase
> SET
> reporter = '$_POST[reporter]',
> date = '$_POST[date]',
> district = '$_POST[district]',
> village = '$_POST[village]',
> school = '$_POST[school]',
> address = '$_POST[address]',
> teacher = '$_POST[teacher]',
> teacher_contact = '$_POST[teacher_contact]',
> alt_teach = '$_POST[alt_teach]',
> alt_teach_contact = '$_POST[alt_teach_contact]',
> gps = '$_POST[gps]',
> etc
> etc
> etc
> 
> 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.

If you don't use a framework, you must type in a SET statement for every single
column you want to save in the database. There is no getting around it. You must do
it at least once for every table.

You would normally do even more: apply some test/validation on every field, and make
sure required fields are not empty, and number fields contain numbers...

You do this nowadays with prepared statements, to be protected against SQL injection
attacks.

If you want generic code, you must define the fields and their properties somewhere
else in your code, this is normally not less work.

/Str.

[toc] | [prev] | [next] | [standalone]


#6089

FromIRC <ircadhikari@gmail.com>
Date2012-05-17 04:14 -0700
Message-ID<c106fd66-ca62-45e4-ba22-89eecdaef26d@8g2000vbu.googlegroups.com>
In reply to#6087
On May 16, 9:48 pm, "M. Strobel" <sorry_no_mail_h...@nowhere.dee>
wrote:
> Am 16.05.2012 16:12, schrieb apavl...@gmail.com:
>
>
>
>
>
>
>
>
>
> > 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.
>
> > ex
>
> > $sql = "update myhugedatabase
> > SET
> > reporter = '$_POST[reporter]',
> > date = '$_POST[date]',
> > district = '$_POST[district]',
> > village = '$_POST[village]',
> > school = '$_POST[school]',
> > address = '$_POST[address]',
> > teacher = '$_POST[teacher]',
> > teacher_contact = '$_POST[teacher_contact]',
> > alt_teach = '$_POST[alt_teach]',
> > alt_teach_contact = '$_POST[alt_teach_contact]',
> > gps = '$_POST[gps]',
> > etc
> > etc
> > etc
>
> > 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.
>
> If you don't use a framework, you must type in a SET statement for every single
> column you want to save in the database. There is no getting around it. You must do
> it at least once for every table.
>
> You would normally do even more: apply some test/validation on every field, and make
> sure required fields are not empty, and number fields contain numbers...
>
> You do this nowadays with prepared statements, to be protected against SQL injection
> attacks.
>
> If you want generic code, you must define the fields and their properties somewhere
> else in your code, this is normally not less work.
>
> /Str.

I agree with Strobel.

You have to create a function once (which can be generic) which will
process your update statement and you just pass an array of $_POST
data into that function and it will process it accordingly.

It may be worthwhile to look into ADODB database library found on php
http://adodb.sourceforge.net/

[toc] | [prev] | [next] | [standalone]


#6096

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-05-18 10:49 +0200
Message-ID<4fb60d06$0$6970$e4fe514c@news2.news.xs4all.nl>
In reply to#6089
On 5/17/2012 1:14 PM, IRC wrote:
> On May 16, 9:48 pm, "M. Strobel"<sorry_no_mail_h...@nowhere.dee>
> wrote:
>> Am 16.05.2012 16:12, schrieb apavl...@gmail.com:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> 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.
>>
>>> ex
>>
>>> $sql = "update myhugedatabase
>>> SET
>>> reporter = '$_POST[reporter]',
>>> date = '$_POST[date]',
>>> district = '$_POST[district]',
>>> village = '$_POST[village]',
>>> school = '$_POST[school]',
>>> address = '$_POST[address]',
>>> teacher = '$_POST[teacher]',
>>> teacher_contact = '$_POST[teacher_contact]',
>>> alt_teach = '$_POST[alt_teach]',
>>> alt_teach_contact = '$_POST[alt_teach_contact]',
>>> gps = '$_POST[gps]',
>>> etc
>>> etc
>>> etc
>>
>>> 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.
>>
>> If you don't use a framework, you must type in a SET statement for every single
>> column you want to save in the database. There is no getting around it. You must do
>> it at least once for every table.
>>
>> You would normally do even more: apply some test/validation on every field, and make
>> sure required fields are not empty, and number fields contain numbers...
>>
>> You do this nowadays with prepared statements, to be protected against SQL injection
>> attacks.
>>
>> If you want generic code, you must define the fields and their properties somewhere
>> else in your code, this is normally not less work.
>>
>> /Str.
>
> I agree with Strobel.
>
> You have to create a function once (which can be generic) which will
> process your update statement and you just pass an array of $_POST
> data into that function and it will process it accordingly.
>
> It may be worthwhile to look into ADODB database library found on php
> http://adodb.sourceforge.net/

+1 on adodb. I have always used it with pleasure: a simple clear approach.

But prepared statements are just one way to avoid SQL injection.
One could also simply use the right string-escape-function.
Both approaches are OK.

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#6088

Frombill <nobody@spamcop.net>
Date2012-05-17 07:09 -0400
Message-ID<x76dndyBOK3nQSnSnZ2dnUVZ_vadnZ2d@cablespeedmi.com>
In reply to#6078
On 5/16/2012 10:12 AM, apavluck@gmail.com wrote:
> 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.
>
> ex
>
> $sql = "update myhugedatabase
> SET
> reporter = '$_POST[reporter]',
> date = '$_POST[date]',
> district = '$_POST[district]',
> village = '$_POST[village]',
> school = '$_POST[school]',
> address = '$_POST[address]',
> teacher = '$_POST[teacher]',
> teacher_contact = '$_POST[teacher_contact]',
> alt_teach = '$_POST[alt_teach]',
> alt_teach_contact = '$_POST[alt_teach_contact]',
> gps = '$_POST[gps]',
> etc
> etc
> etc
>
> 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.

if you have all the data in the row (or hidden fields) you can 
just do a REPLACE instead of the UPDATE

bill

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web