confirm delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

confirm delete

Post by new to php »

hi

I have admin page where user can delete a person from the databse. Im using email address as primary key. what I'm trying to do is when user hits 'Delete' button on the form he/she asked to confirm delete "Are you sure" if they hits 'Yes' then record is deleted from the databse.

here is the code

delete.php

<form action="confirm.php" method="post">
Email: <input type="text" name="email" size="40" maxlength="40" ><br>
<input type="submit" name="delete" value="Delete">
</form>


confirm.php

if ($delete == "Delete")

{
<h3>Are You Sure</h3>
<form action="edit.php" method="post" >
<input type="submit" name="delete" value="Yes">
<input type=hidden name=email value="$email">
</form>
}


edit.php

if ($delete == "Yes")
{
$query = "delete from Names "
."where Email='$email'";

print "<h2>$email has been deleted from the databese </h2>\n";
}

i get "$email has been deleted from the databese" but when look in the databse record is still there.

I think this is where the problem is but con't work it out. Please help

<input type="submit" name="delete" value="Yes">
<input type=hidden name=email value="$email">

Thanx
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's because you have to actually run the query through your database. Assuming you use MySQL you would want to do something like:

Code: Select all

if ($delete == "Yes") { 
    // connect to database server
    @mysql_connect($host, $user, $pass) or die(mysql_error());
    // select database
    @mysql_select_db($db) or die(mysql_error());
    
    $sql = "DELETE FROM Names WHERE Email='$email'"; 
    // query the database
    @mysql_query($sql) or die(mysql_error().'&lt;p&gt;'.$sql.'&lt;/p&gt;');
    echo '&lt;h2&gt;'.$email.' has been deleted from the database&lt;/h2&gt;'; 
}
Have a look at the manual information on mysql_connect(), mysql_select_db() and mysql_query().

Mac
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

Yes im using MySQL.

I should have pointed out that in edit.php im including header.php which connects to my database.

<?php
function dbconnect ($dbname="",$user="",$password="",$server="localhost")
{
if (!($mylink = mysql_connect($server,$user,$password)))
{
print "<h3>could not connect to database</h3>\n";
exit;
}
mysql_select_db($dbname);
}


dbconnect("");
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Then you just need to add the call to mysql_query() which will execute the SQL statement.

Mac
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I have admin page where user can delete a person from the databse. Im using email address as primary key. what I'm trying to do is when user hits 'Delete' button on the form he/ she asked to confirm delete "Are you sure" if they hits 'Yes' then record is deleted from the databse.
I think JavaScript is perfectly suited for this. It's as simple as the javascript alert() function, which places a box in front of the user and tells them to confirm what they are about to do. Using PHP, you have to make a trip back to the server, process the info, then another trip back to the user, where she decides that the delete is OK, then the final trip back to the server to delete (or not) the info.

Check into it. Also, javascript isn't really difficult if you're allready programming in PHP as they are both using a C type syntax. Logic is logic. The DCOM stuff will be a little weird at first.

Cheers,
BDKR
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

"Then you just need to add the call to mysql_query() which will execute the SQL statement."


I've done that still not working
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Does it give you any errors? What information do you get if you do:

Code: Select all

echo 'Affected rows: '.mysql_affected_rows();
after the query.

http://www.php.net/manual/en/function.m ... d-rows.php

Mac
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

No errors
I get:

Affected rows: -1
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

According to the documentation on mysql_affected_rows():
If the last query failed, this function will return -1
so your query is failing - do you have an or die() statement at the end of the mysql_query() call? E.g.

Code: Select all

@mysql_query($query) or die(mysql_error());
if you use mysql_error() in the or die() statement you may get a reason for the query failing. If there is no error then make sure the query looks alright by doing:

Code: Select all

echo $query;
just in case $email isn't getting passed properly.

Mac
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

i have added this in my code

mysql_query($query) or die(mysql_error());

Now i getting "Query was empty"
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does the full code for the deletion part of edit.php currently look like?

Mac
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

if ($delete == "Yes")
{
mysql_query($query) or die(mysql_error());
$query= mysql_query ("delete from Names"
."where Email='$email'");

echo 'Affected rows: '.mysql_affected_rows();

print "<h2>$email has been deleted from the databese </h2>\n";
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to change this:

Code: Select all

mysql_query($query) or die(mysql_error()); 
$query= mysql_query ("delete from Names" 
."where Email='$email'");
to this

Code: Select all

$query = "DELETE FROM Names WHERE Email='$email'";
@mysql_query($query) or die(mysql_error());
Mac
new to php
Forum Newbie
Posts: 11
Joined: Fri Nov 29, 2002 6:16 am

Post by new to php »

still doesn't work
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Does it give you the same error? Have you done:

Code: Select all

echo $query;
to check the SQL query?

Mac
Post Reply