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

I've got some code that gets a name from a form, checks a MySQL database for duplications, if no duplications found it is supposed to write the new name as a new record. I say supposed to because it stops at that point and tells me
syntax error at /usr/lib/cgi-bin/addoffice.cgi line 62, near "$sth = $ +dbh->prepare($stmt) || die "" Can't find string terminator '"' anywhere before EOF at /usr/lib/cgi-b +in/addoffice.cgi line 63.
.
It sounds to me like there is a string in the wrong place but I can't see it. In fact, if I run the script from terminal I get
<!-- warning: String found where operator expected at ./addoffice.cgi +line 59, near "$sth = $dbh&#65533;&#65533;prepare($stmt) || die "" (M +ight be a runaway multi&#65533;line "" string starting on line 57) -- +>

What confounds me is that the code worked fine until I added the second MySQL query but the second query syntax is a copy and paste of the first.
I have tried closing and reopening the database connection in between the two queries but got the same results. I've posted the code below. Does anyone know what I've stuffed up?
# Prepare and execute the SQL query $sth = $dbh->prepare($stmt) || die "prepare: $stmt: $DBI::errstr"; $sth->execute($office) || die <br/>"execute: $stmt: $DBI:: +errstr"; $sth->bind(undef,/$num); $sth->fetchrow(); print start_html; if ($num){ #if a match has been found $message= "The office of ".$office." is already listed.<br +>"; $url = "http://stgeorges.com/admin/addoffice.html?office=$ +office&msg=$message";<br/> $q =>print "<meta http-equiv='refresh' content='0;URL=$url +'>"; } else{ # Create the query statement - Insert the new office into the database $stmt = "INSERT into officers (off_name) values (?)"; # Prepare and execute the SQL query $sth = $dbh->prepare($stmt) || die "prepare: $stmt: $DBI:: +errstr"; $sth->execute($office) || die "execute: $stmt: $DBI::errst +r"; $sth->fetchrow(); } print end_html;

Replies are listed 'Best First'.
Re: Problem with second MySQL Query
by cosmicperl (Chaplain) on Oct 07, 2007 at 23:40 UTC
    Shouldn't
    $sth->bind(undef,/$num);
    be:-
    $sth->bind(undef,\$num);

    Would really help if you pointed out which lines are 57, 59 and 63.

    Like the error says it could be an unmatched " from further up than you've given us.

    Lyle
      Yes /$num should be \$num - so I changed it.
      That solved the problem.
      I've only just managed to get my hands on a copy of the Camel and I'm trying to improve my understanding of Perl by reading/applying it. Could using '/' instead of '\' have been interpreted as a '"'?
      By the way, point taken about identifying the lines in question.
      Thanks for your help.
Re: Problem with second MySQL Query
by rhesa (Vicar) on Oct 08, 2007 at 00:07 UTC
    Have you considered putting a unique index on the officers field? That way the database will do the check for you, so you only have to try the insert.
      Thanks rhesa,
      I haven't really looked into those aspects of using a database. I've had my hands full getting my head around Perl so I've stuck to the basics with MySQL. It sounds like a sensible approach though so I'll have to spend a bit more time brushing up on databases.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Problem with second MySQL Query
by Cop (Initiate) on Oct 08, 2007 at 02:36 UTC

    That unicode replacement character makes me think that you have garbage there. Remove the line and retype might help.

      Thanks. I wondered what that might mean but I had already retyped the whole block of code before running it through terminal.