Forum Moderators: coopster & phranque

Message Too Old, No Replies

Delete function from database

How to make a delete function

         

MartinTygsen

7:24 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Hello

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

bennymack

7:52 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



First off, I'd like to point out that this

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!

MartinTygsen

8:06 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



thanks for a great answer, it really is helpfull, I will try it now or maybe tomorrow its late here in Denmark.

I will write as soon as i test it.

Thanks

MartinTygsen

8:12 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



In this line

$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

bennymack

8:15 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Whoops, it should be like so:

$rows_deleted = $dbh->do(q[DELETE FROM bookmarks WHERE id =?], undef, $id) ¦¦ print "Problem deleting \$id=$id $dbh->errstr";

You might have to tweak some other parts of the script. Such as adding a conditional so it only deletes when there is a delete variable.

MartinTygsen

8:21 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



okay thanks, I will go to bed now and look at it tomorrow.

I really appreciate your help.

MartinTygsen

1:34 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



Hello again,

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 :)

SeanW

11:28 am on Aug 26, 2005 (gmt 0)

10+ Year Member



[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

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

MartinTygsen

11:33 am on Aug 26, 2005 (gmt 0)

10+ Year Member



$rows_deleted = $dbh->do(q[DELETE FROM bookmarks WHERE id =?], undef, $id) ¦¦ print "Problem deleting \$id=$id $dbh->errstr";

this is line 18, and there is no single quotes? can it be ¦¦?

MartinTygsen

11:38 am on Aug 26, 2005 (gmt 0)

10+ Year Member



Okay it was ¦¦

When i changed them it worked, or no.

Ir doesnt give me any errors anymore, but it doesnt delete anything.

Where should I place the code eaxctly?