Multiple Update SQL Statements Execution

Multiple Update SQL Statements Execution

am 19.11.2008 14:55:29 von Alice Wei

--_930be3f5-e720-4144-8e24-3bc92d0d7215_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Hi,

I am inquiring on this list to see if it is possible to create a script t=
hat takes multiple update statements without my having to write one "SQL" s=
tatement for each of the updates.=20

I have a scenario of which I create a table of some sort with some existi=
ng information using Flex=2C and what I am told by my client is that no mat=
ter how many records there are on the screen=2C the users should be able to=
update any up to all the entries by simply pushing a button. I use Microso=
ft SQL=2C which I think that it does allow multiple update query execution.=
The problem is that I might have to come up with some method to accept all=
the "POST" variables the user provides into the script.=20

Could anyone please give me some guidance on what kind of function I mig=
ht use=2C or whether or not it is possible I can create a script that accep=
ts as many "POST" variables as the users POST?=20

Thanks a lot for your help.

Alice

=20

____________________________________________________________ _____
Search from any Web page with powerful protection. Get the FREE Windows Liv=
e Toolbar Today!
http://get.live.com/toolbar/overview=

--_930be3f5-e720-4144-8e24-3bc92d0d7215_--

Re: Multiple Update SQL Statements Execution

am 19.11.2008 21:23:33 von Fergus Gibson

On Wed, Nov 19, 2008 at 5:55 AM, Alice Wei wrote:
> I am inquiring on this list to see if it is possible to create a script that takes multiple update statements without my having to write one "SQL" statement for each of the updates.

I'm not sure I understand your question. It is certainly possible to
write one query that updates multiple rows at once. In other cases,
you can use prepared statements and bound variables. If all you need
to do is repeat a query of the same structure with different values, a
prepared statement would be faster and mean cleaner code than sending
repeated queries.

Without more specific info from you, I don't think I can give a better
answer than this. I've never worked with Microsoft SQL Server, so I
doubt there's anything I can tell you about that in particular.


> I have a scenario of which I create a table of some sort with some existing information using Flex, and what I am told by my client is that no matter how many records there are on the screen, the users should be able to update any up to all the entries by simply pushing a button. I use Microsoft SQL, which I think that it does allow multiple update query execution. The problem is that I might have to come up with some method to accept all the "POST" variables the user provides into the script.

Let's see. If your POST includes the IDs of the rows you want to
change and the value you want to update, it could go something like
this. Note that I haven't tested it, so it might contain an error.
I'm just trying to provide an illustration of the approach.


/*

SKIPPED: connect to your database as appropriate. Below I show using
the PDO extension to escape the incoming data using the quote()
method. If you are using the mssql extension instead, there is no
escape function (!) so you'll have to decide how best to escape the
data. That's reason enough for me to prefer PDO.

If you don't know what I'm talking about here, you should study SQL
injection until you're sure you fully understand. Otherwise you will
produce very vulnerable code.

*/

$sql = "UPDATE sometable SET somecolumn = '" .
$pdo->quote($_POST['field']) . "' WHERE id IN ("
. implode(',' $_POST['id']) . ")";

/*

Send this query to your database as appropriate. It will set
'somecolumn' to the value of $_POST['field'] where the ID is in the
list. In this case the form should submit the $_POST['id'] value as
an array, which can be done by using setting the HTML name attribute
to id[] (e.g. name="id[]").

*/

?>

Does this help?

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple Update SQL Statements Execution

am 20.11.2008 19:25:14 von Goltsios Theodore

> Hi,
>
> I am inquiring on this list to see if it is possible to create a script that takes multiple update statements without my having to write one "SQL" statement for each of the updates.
>

If you want to reuse the same SQL query by just changing the parameters
you may consider using the PDO's driver for mssql an make prepared
statements.
Never used MsSQL so I am not sure that prepared statements work the same
as with other PDO drivers.

check this on the manual:
http://www.php.net/manual/en/pdo.prepare.php

You may also find instructions on how to install and use PDO and the
drivers it support on the manual as well.

> I have a scenario of which I create a table of some sort with some existing information using Flex, and what I am told by my client is that no matter how many records there are on the screen, the users should be able to update any up to all the entries by simply pushing a button. I use Microsoft SQL, which I think that it does allow multiple update query execution. The problem is that I might have to come up with some method to accept all the "POST" variables the user provides into the script.
>

You can do that by using the $_POST array which stores all the posted data.

check this:
http://www.php.net/manual/en/reserved.variables.post.php

> Could anyone please give me some guidance on what kind of function I might use, or whether or not it is possible I can create a script that accepts as many "POST" variables as the users POST?
>
> Thanks a lot for your help.
>
> Alice
>
>

Those two elementary howtos may be of some help:
http://www.w3schools.com/php/php_post.asp
http://www.tizag.com/phpT/postget.php


If all these seem too elementary for you then try to describe your
problem in a more detailed manner. Better questions get better answers.

--
Thodoris


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Multiple Update SQL Statements Execution

am 25.11.2008 14:53:44 von Alice Wei

--_b5b3a2da-ce14-4af3-af17-7edf386053be_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Hi,
=20
Thanks for the replies=2C guys. I could finally come up with the actual c=
ode to do multiple updates. Just like some of you said=2C I had to name my =
index variables for it to distinguish between different elements.=20
=20
for($i =3D 1=3B$i <=3D $total=3B $i++){
=20
$a =3D $_POST['a'.$i]=3B $b =3D $_POST['b'.$i]=3B $c =3D $_POST['c'.$=
i]=3B
$d =3D $_POST['d'.$i]=3B $e =3D $_POST['e'.$i]=3B
$sql =3D "UPDATE table SET market=3D'$a'=2CIM_accept=3D'$b'=2C IM_def=
er=3D'$c'=2C CR_accept=3D'$d'=2C FC_accept=3D'$e'=20
WHERE scenario_id=3D'$scenario_id'"=3B
echo "$sql"=3B
$result2=3Dmssql_query($sql) or die ("Can't execute $sql")=3B
}

However=2C this might be out of the question I have been asking here. Has a=
nyone tried to do this type of stuff using Flex and PHP?=20

Alice

> Date: Wed=2C 19 Nov 2008 12:23:33 -0800
> From: fgibson75@gmail.com
> To: php-db@lists.php.net
> Subject: Re: [PHP-DB] Multiple Update SQL Statements Execution
>=20
> On Wed=2C Nov 19=2C 2008 at 5:55 AM=2C Alice Wei wr=
ote:
> > I am inquiring on this list to see if it is possible to create a scrip=
t that takes multiple update statements without my having to write one "SQL=
" statement for each of the updates.
>=20
> I'm not sure I understand your question. It is certainly possible to
> write one query that updates multiple rows at once. In other cases=2C
> you can use prepared statements and bound variables. If all you need
> to do is repeat a query of the same structure with different values=2C a
> prepared statement would be faster and mean cleaner code than sending
> repeated queries.
>=20
> Without more specific info from you=2C I don't think I can give a better
> answer than this. I've never worked with Microsoft SQL Server=2C so I
> doubt there's anything I can tell you about that in particular.
>=20
>=20
> > I have a scenario of which I create a table of some sort with some exi=
sting information using Flex=2C and what I am told by my client is that no =
matter how many records there are on the screen=2C the users should be able=
to update any up to all the entries by simply pushing a button. I use Micr=
osoft SQL=2C which I think that it does allow multiple update query executi=
on. The problem is that I might have to come up with some method to accept =
all the "POST" variables the user provides into the script.
>=20
> Let's see. If your POST includes the IDs of the rows you want to
> change and the value you want to update=2C it could go something like
> this. Note that I haven't tested it=2C so it might contain an error.
> I'm just trying to provide an illustration of the approach.
>=20
> >=20
> /*
>=20
> SKIPPED: connect to your database as appropriate. Below I show using
> the PDO extension to escape the incoming data using the quote()
> method. If you are using the mssql extension instead=2C there is no
> escape function (!) so you'll have to decide how best to escape the
> data. That's reason enough for me to prefer PDO.
>=20
> If you don't know what I'm talking about here=2C you should study SQL
> injection until you're sure you fully understand. Otherwise you will
> produce very vulnerable code.
>=20
> */
>=20
> $sql =3D "UPDATE sometable SET somecolumn =3D '" .
> $pdo->quote($_POST['field']) . "' WHERE id IN ("
> . implode('=2C' $_POST['id']) . ")"=3B
>=20
> /*
>=20
> Send this query to your database as appropriate. It will set
> 'somecolumn' to the value of $_POST['field'] where the ID is in the
> list. In this case the form should submit the $_POST['id'] value as
> an array=2C which can be done by using setting the HTML name attribute
> to id[] (e.g. name=3D"id[]").
>=20
> */
>=20
> ?>
>=20
> Does this help?
>=20
> --=20
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe=2C visit: http://www.php.net/unsub.php
>=20

____________________________________________________________ _____
Check the weather nationwide with MSN Search: Try it now!
http://search.msn.com/results.aspx?q=3Dweather&FORM=3DWLMTAG =

--_b5b3a2da-ce14-4af3-af17-7edf386053be_--