Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Udate table while using placeholders 3

Status
Not open for further replies.

eWish

Programmer
Aug 12, 2006
16
US
Can you use placeholders when using the UPDATE statement? I have tried the following, however, it does not give an error nor does it update the table.

Code:
$sql = $dbh->prepare("UPDATE sometable SET (name, category) WHERE id=(?) VALUES (?,?)" , undef, $params{'name'}, $params{'category'});
$sql->execute($params{'id'});
$sql->finish();
 
Change your code to this:

Code:
$sql = $dbh->prepare("UPDATE sometable SET name=?, category=? WHERE id=?");
$sql->execute($params{'name'}, $params{'category'}, $params{'id'});
$sql->finish();
 
Thank you MillerH. I tried that as well as this and still no errors, nor does it update the table.
Code:
$sql = $dbh->prepare("UPDATE sometable SET name=?, category=? WHERE id=$params{'id'}");
$sql->execute($params{'name'}, $params{'category'});
$sql->finish();
Also tried this.
Code:
$sql = $dbh->prepare("UPDATE sometable SET name=?, category=? WHERE id=1");
$sql->execute($params{'name'}, $params{'category'});
$sql->finish();
 
check that there are actual values prior to execution??

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
my $rv = $sql->execute();
print "return value = $rv";
What is the return code? Also, the DBI doc mentions something about autocommit. If it is switched off, you may find that you need to commit before you can see the updates from another connection.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
For debugging purposes, tail the SQL log to see if the command is actually registering. If it's not, remove the placeholders and see if that registers. Proceed appropriately from there.

Also, follow stevexff's advice concerning returning number of rows modified.
 
The problem has been resolved. Short Answer - The value for id was empty. I decided to used a hidden field in lieu of the url as I originally planned.

@MillerH - Your code did work fine once I got the id worked out.

@Paul - The is was the only param without a value, all other's were fine.

@Stevexff - Your idea of the return value was key in tracking down the error. Along with changing PrintError to RaiseError.

I appreciate everyone's help. Thank You!

--ewish
 
All you have to do is Ax;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Good work.

It sounds like you've learned a few new tools for debugging the use of DBI. Remember though, that tailing the SQL log also makes for a very good sanity check. It would have showed you the SQL query with values for name and category, but none for id.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top