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; }
In reply to DBI Select and Update by nutate
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |