FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » update mysql without manually tying SET statements
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: update mysql without manually tying SET statements [message #178176 is a reply to message #178161] Thu, 17 May 2012 18:06 Go to previous messageGo to previous message
Alex Pavluck is currently offline  Alex Pavluck
Messages: 5
Registered: April 2011
Karma:
Junior Member
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(at)gmail(dot)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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: table appears with empty rows, and row data appears before table. Code seems ok. What is going on?
Next Topic: Dynamic water marking (redirect?)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 00:35:56 GMT 2024

Total time taken to generate the page: 0.06996 seconds