silent11 has asked for the wisdom of the Perl Monks concerning the following question:

I can't get the update section to work correctly. What's the matter with this code?
#!/usr/bin/perl -w # commit.pl # CREATE table people ( # ID varchar(10) not null primary key, # NAME varchar(100) DEFAULT '', # EMAIL varchar(100) DEFAULT '', # STREET varchar(100) DEFAULT '', # CITY varchar(100) DEFAULT '', # STATE varchar(10) DEFAULT '', # ZIP varchar(20) DEFAULT '', # COUNTRY varchar(60) DEFAULT '', # ADD_DATE date # ); use strict; use CGI ':standard'; use DBI; my $name = param('name') || ''; my $id = param('id') || ''; my $email = param('email') || ''; my $city = param('city') || ''; my $state = param('state') || ''; my $zip = param('zip') || ''; my $street = param('street') || ''; my $country = param('country')|| ''; if ( param('go') eq 'Commit Changes' ) { print header; my $dbh = DBI->connect('DBI:mysql:3s','root','') || die $DBI::errs +tr; my $sth = $dbh->prepare( q{ UPDATE people SET NAME = ?, SET EMAIL = ?, SET STREET = ?, SET CITY = ?, SET STATE = ?, SET ZIP = ?, SET COUNTRY = ?, where ID = $id } ) || die $DBI::errstr; $sth->execute( $name , $email , $street , $city , $state , $zip , $cou +ntry ) || print $DBI::errstr ; $sth->finish(); $dbh->disconnect(); print "$name $id $email $city $state $zip $street $country" ; }


-Silent11

Replies are listed 'Best First'.
Re: SQL UPDATE and DBI for MySQL
by fenonn (Chaplain) on Jun 19, 2002 at 19:31 UTC
    the correct syntax for UPDATE is something like:
    UPDATE tbl_name SET col_name1=expr1, col_name2=expr2, ... WHERE where_definition
Re: SQL UPDATE and DBI for MySQL
by dws (Chancellor) on Jun 19, 2002 at 19:18 UTC
    I can't get the update section to work correctly. What's the matter with this code?

    You got the (SQL) syntax wrong for the UPDATE query. Consult your nearest SQL documentation.

Re: SQL UPDATE and DBI for MySQL
by busunsl (Vicar) on Jun 20, 2002 at 11:42 UTC
    dws and fennon are right, it's the SQL-syntax.
    But it's really hard to spot the error:

    There must be no comma before the WHERE clause.

Re: SQL UPDATE and DBI for MySQL
by lachoy (Parson) on Jun 20, 2002 at 14:22 UTC

    Additionally, you need to interpolate your query by using a qq instead of a q, since this will pass the character sequence $id rather than the variable value to the database.

    For easier debugging, set $dbh->{RaiseError} = 1 after you create your database handle. This will throw a die on any errors, and the message thrown will include something helpful like ERROR:  parser: parse error at or near "where".

    Chris
    M-x auto-bs-mode