By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Dynamic Form Update

P: n/a
I have the following form in which i need to updateitems.
The problem is that i do not know how many elements will be in the form
(generated dynamically)

eg
<input type = "hidden" name ="id1" value="1"><input type ="qty1" value ="1">
<input type = "hidden" name ="id2" value="2"><input type ="qty2" value ="1">
<input type = "hidden" name ="id3" value="3"><input type ="qty3" value ="5">
<input type = "hidden" name ="idn" value="n"><input type ="qtyn" value ="1">
how do i generate an array for a foreach loop to update the values in PHP
so that i can use it to create a Update query
eg
///begin loop
$sql = "UPDATE `table` SET `qty` = `$qtyn` WHERE `id` = `$idn`";
// end loop
many thanks for any help
Craig
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In comp.lang.php Craig Keightley <do**@spam.me> wrote:
<input type = "hidden" name ="idn" value="n"><input type ="qtyn" value ="1">
This doens't make any sense in html.
how do i generate an array for a foreach loop to update the values in PHP


<input type='hidden' name='qty[n]' value='x'>

should give you an array in _GET/_POST with the name qty, keys are 'n'
and values are 'x'.

FUP: comp.lang.php
Jul 17 '05 #2

P: n/a
My Mistake, the HTML should read as:
<input type = "hidden" name ="idn" value="n"><input type ="text" name="qtyn"
value ="1">

"Daniel Tryba" <pa**********@invalid.tryba.nl> wrote in message
news:42***********************@news6.xs4all.nl...
In comp.lang.php Craig Keightley <do**@spam.me> wrote:
<input type = "hidden" name ="idn" value="n"><input type ="qtyn" value
="1">


This doens't make any sense in html.
how do i generate an array for a foreach loop to update the values in
PHP


<input type='hidden' name='qty[n]' value='x'>

should give you an array in _GET/_POST with the name qty, keys are 'n'
and values are 'x'.

FUP: comp.lang.php

Jul 17 '05 #3

P: n/a
Sorted: Many thanks for the nudge

if(isset($_POST['update']) && ($_POST['update'] == "true")){
mysql_select_db($database_conn, $conn);
foreach ($qty as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";
mysql_query($sql, $conn) or die(mysql_error());
}
header("Location: basket.php");
}

"Daniel Tryba" <pa**********@invalid.tryba.nl> wrote in message
news:42***********************@news6.xs4all.nl...
In comp.lang.php Craig Keightley <do**@spam.me> wrote:
<input type = "hidden" name ="idn" value="n"><input type ="qtyn" value
="1">


This doens't make any sense in html.
how do i generate an array for a foreach loop to update the values in
PHP


<input type='hidden' name='qty[n]' value='x'>

should give you an array in _GET/_POST with the name qty, keys are 'n'
and values are 'x'.

FUP: comp.lang.php

Jul 17 '05 #4

P: n/a

--ScareCrowe
"Daniel Tryba" <pa**********@invalid.tryba.nl> wrote in message
news:42***********************@news6.xs4all.nl...
In comp.lang.php Craig Keightley <do**@spam.me> wrote:
<input type = "hidden" name ="idn" value="n"><input type ="qtyn" value
="1">
This doens't make any sense in html.
how do i generate an array for a foreach loop to update the values in
PHP
<input type='hidden' name='qty[n]' value='x'>

should give you an array in _GET/_POST with the name qty, keys are 'n'
and values are 'x'.

FUP: comp.lang.php


Actually, it makes sense to me though. ( <input type="hidden" name="idn"
value="n"> is correct HTML, although it is spaced incorrectly, it is just as
good as spelling doesn't like so: doens't ;P )
I think he is asking how to insert multiple entries into a DB passed from a
form.
Assuming you know how many entries there will be, just iterate thru it with
a for loop. If you have 10 entries, for($a=0;$a<10;$a++) etc etc.

Otherwise:

I do agree using form element variables as an array like id[1], id[2]
instead of id1, id2. This way, when you go through the loop you can do
something like this:
$id_count = count($_POST['id']);
for($b=0;$b<$id_count;$b++){
mysql_query("INSERT INTO blahblah values ($_POST[id][$b],
$_POST[qty][$b]");
}
Not CnP, but you should get the point from that!

--ScareCrowe
Jul 17 '05 #5

P: n/a
Following on from ScareCrowe's message. . .
I do agree using form element variables as an array like id[1], id[2]
instead of id1, id2. This way, when you go through the loop you can do
something like this:
$id_count = count($_POST['id']);
for($b=0;$b<$id_count;$b++){
mysql_query("INSERT INTO blahblah values ($_POST[id][$b],
$_POST[qty][$b]");
}


This is lots of inserts (or updates) when one would suffice.
INSERT INTO aTable name,address,telno VALUES ('Fred Smith', '1 High
St.','01376 515151')

So do something like this to economise on DB access
$vals = implode("','",array_values($POST));
$sql = "Insert .....values('$vals')";
(NB This isn't meant to be real working code - there are lots of issues
with it - just an illustration.)

--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #6

P: n/a
Following on from ScareCrowe's message. . .
I do agree using form element variables as an array like id[1], id[2]
instead of id1, id2. This way, when you go through the loop you can do
something like this:
$id_count = count($_POST['id']);
for($b=0;$b<$id_count;$b++){
mysql_query("INSERT INTO blahblah values ($_POST[id][$b],
$_POST[qty][$b]");
}


This is lots of inserts (or updates) when one would suffice.
INSERT INTO aTable name,address,telno VALUES ('Fred Smith', '1 High
St.','01376 515151')

So do something like this to economise on DB access
$vals = implode("','",array_values($POST));
$sql = "Insert .....values('$vals')";
(NB This isn't meant to be real working code - there are lots of issues
with it - just an illustration.)

--
PETER FOX Not the same since the bottom fell out of the bucket business
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #7

P: n/a
Hello everyone,
I have a somewhat same problem.
Craig, from your code

foreach ($qty as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";

==> qty=$key

$key contains the values of qty passed from last page, with me qty for
all products is different....like.

qty1 = 2
qty2 = 7
qty3 = 4
and so on

I have not been able to pass the primary key to update the DB table so
i use

foreach ($_POST as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";

but even the the primary key is missing and update is not done on all
products.

please help.
Thanks a lot.
Jaunty Edward

Jul 17 '05 #8

P: n/a
Hello everyone,
I have a somewhat same problem.

Craig, from your code:

foreach ($qty as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";
==> qty=$key

$key contains the values of qty passed from last page, unlike you for
me qty for all products is different....like.

qty1 = 2
qty2 = 7
qty3 = 4
and so on

I have not been able to pass the primary key to update the DB table so
i use

foreach ($_POST as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";

but even then the primary key is missing and update is not done on all
products, somehow product reference is not passed.

please help.
Thanks a lot.
Jaunty Edward

Jul 17 '05 #9

P: n/a
Hello everyone,
I have a somewhat same problem.

Craig, from your code:

foreach ($qty as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";
==> qty=$key

$key contains the values of qty passed from last page, unlike you for
me qty for all products is different....like.

qty1 = 2
qty2 = 7
qty3 = 4
and so on

I have not been able to pass the primary key to update the DB table so
i use

foreach ($_POST as $v => $key) {
$sql = "UPDATE tblCart SET qty=$key WHERE prodRef=$v AND
cookieRef='".$ID."'";

but even then the primary key is missing and update is not done on all
products, somehow product reference is not passed.

please help.
Thanks a lot.
Jaunty Edward

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.