This code snippet solved a problem I had where I needed to update a series of rows in a purchase order database with numbers being stored in cgi variables based on oid (object identification) numbers.
The cgi variables were named via oid's which was convenient at the time. I don't know if databases other than PostgreSQL have oids, but I wouldn't be surprised if they did. Some of the variable and placeholders may be unecessary, but that's why I am posting this, to see other right ways to do this.
$sth = $dbh->prepare(q{SELECT oid from items where orderid = ?}) || +die $dbh->errstr; $sth->execute($orderid) || die $dbh->errstr; my @oids; my $oid; $sth->bind_columns(\$oid); # Binds each column to $oid, through ref +erence my $i; my $max; for ($i = 0; $sth->fetch; $i++) { # The fetch updates $oid to the ne +xt one. $oids[$i] = $oid; $max = $i; } for ($i = 0; $i <= $max; $i++) { my $price = $q->param("item_" . $oids[$i] . "_price"); # Gets pric +e from cgi my $curoid = $oids[$i]; # unecessary...? $sth = $dbh->prepare(q{UPDATE items set price = (?) WHERE oid = (? +)}) || die $dbh->errstr; $sth->execute($price, $curoid) || die $dbh->errstr; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: DBI Select and Update
by Anonymous Monk on May 31, 2000 at 18:38 UTC | |
by KM (Priest) on May 31, 2000 at 18:51 UTC | |
by nutate (Novice) on May 31, 2000 at 19:25 UTC | |
by agoth (Chaplain) on Jun 01, 2000 at 01:47 UTC | |
by Anonymous Monk on May 31, 2000 at 18:40 UTC | |
by agoth (Chaplain) on May 31, 2000 at 18:46 UTC |