I am new at perl/cgi and now I ran into som problems.
I am making a little script like a guestbook, where I store the data in a mysql database.
I have made my script so I can store and show the data, but I want a little funktion so I can delete an entry.
My script
_____________________________________
add an entry
#!c:/perl/bin/perl.exe
use strict;
use CGI ':standard';
use DBI;
print "Content-type: text/html\n\n";
my $DataBaseName = "bookmark";
my $DataBaseHost = "localhost";
my $DataBaseUser = "root";
my $DataBasePass = "";
my $dbh = DBI->connect("DBI:mysql:database=$DataBaseName;host=$DataBaseHost",
"$DataBaseUser",
"$DataBasePass",
{ RaiseError => 1,
AutoCommit => 0 }) ¦¦ die "Unable to connect to $DataBaseHost because $DBI::errstr";
my $title = param('title');
my $link = param('link');
my $descrip = param('descrip');
$dbh->do("INSERT INTO bookmark values(id,'$title','$link','$descrip')");
$dbh->disconnect();
_____________________________________
show all the entries
#!c:/perl/bin/perl.exe
use strict;
use CGI ':standard';
use DBI;
print "Content-type: text/html\n\n";
my $DataBaseName = "bookmark";
my $DataBaseHost = "localhost";
my $DataBaseUser = "root";
my $DataBasePass = "";
my $dbh = DBI->connect("DBI:mysql:database=$DataBaseName;host=$DataBaseHost",
"$DataBaseUser",
"$DataBasePass",
{ RaiseError => 1, AutoCommit => 0 }) ¦¦ die "Unable to connect to $DataBaseHost because $DBI::errstr";
my $sql = "SELECT * FROM bookmark ORDER BY id DESC";
my $sth = $dbh->prepare($sql);
$sth->execute();
while (my @row = $sth->fetchrow_array )
{
my $title = "$row[1]";
my $link = "$row[2]";
my $descrip = "$row[3]";
print qq(<table width="500" height="100" border="0">
<tr>
<td><a href=http://$link>$title<a/></td>
</tr>
<tr>
<td>$descrip</td>
</tr>
<tr>
<td>
<form name="delete" method="post" action="">
<input type="submit" name="delete" value="delete">
</form>
</td>
</tr>
<tr>
<td><HR></td>
</tr>
</table>);
}
$dbh->disconnect();
_____________________________________
I want some tips to how I make the button "delete" work.
Thanks
Yours sincerly
Martin Tygsen
while (my @row = $sth->fetchrow_array )
{
my $title = "$row[1]";
my $link = "$row[2]";
my $descrip = "$row[3]";
can be changed to this
while (my ($id, $title, $link, $descrip) = $sth->fetchrow_array )
Then I'd change the Show All block to:
print qq(<table width="500" height="100" border="0">
<tr>
<td><a href=http://$link>$title<a/></td>
</tr>
<tr>
<td>$descrip</td>
</tr>
<tr>
<td><a href="?delete=$id">Delete me</a>
</td>
</tr>
<tr>
<td><HR></td>
</tr>
</table>);
And finally add this to the Show all page somewhere before it pulls out the remaining items:
my $id = param('id');
my $rows_deleted = $dbh->do(q[DELETE FROM bookmarks WHERE id =?, undef, $id) ¦¦ print "Problem deleting \$id=$id $dbh->errstr";
Congrats on choosing perl!
$rows_deleted = $dbh->do(q[DELETE FROM bookmarks WHERE id =?, undef, $id) ¦¦ print "Problem deleting \$id=$id $dbh->errstr";
is there a ] missing, where do I have to put it?
in the end like
$rows_deleted = $dbh->do(q[DELETE FROM bookmarks WHERE id =?, undef, $id) ¦¦ print "Problem deleting \$id=$id $dbh->errstr"];
Thanks
I tried it but I just cant get it to work.
The apache error log says:
[Wed Aug 24 15:32:53 2005] [error] [client 127.0.0.1] Premature end of script headers: c:/appserv/www/cgi-bin/index2.pl
[Wed Aug 24 15:32:53 2005] [error] [client 127.0.0.1] Unrecognized character \\xA6 at c:\\appserv\\www\\cgi-bin\\index2.pl line 18.\n
I really dont know what I am doing wrong, its probably me that is stupid I guess :)
What's on line 18? Rough guess is those single quotes, and that you cut and pasted this script from a web page. Manually change both quotes using your keyboard to ' and then try again. I suspect you'll have to do this for all single quotes.
Sean