Page 1 of 2
confirm delete
Posted: Fri Nov 29, 2002 6:16 am
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
Posted: Fri Nov 29, 2002 6:22 am
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().'<p>'.$sql.'</p>');
echo '<h2>'.$email.' has been deleted from the database</h2>';
}
Have a look at the manual information on
mysql_connect(),
mysql_select_db() and
mysql_query().
Mac
Posted: Fri Nov 29, 2002 6:56 am
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("");
?>
Posted: Fri Nov 29, 2002 7:01 am
by twigletmac
Then you just need to add the call to mysql_query() which will execute the SQL statement.
Mac
Posted: Fri Nov 29, 2002 7:09 am
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
Posted: Fri Nov 29, 2002 7:11 am
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
Posted: Fri Nov 29, 2002 7:30 am
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
Posted: Fri Nov 29, 2002 8:47 am
by new to php
No errors
I get:
Affected rows: -1
Posted: Fri Nov 29, 2002 8:53 am
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:
just in case $email isn't getting passed properly.
Mac
Posted: Fri Nov 29, 2002 9:05 am
by new to php
i have added this in my code
mysql_query($query) or die(mysql_error());
Now i getting "Query was empty"
Posted: Fri Nov 29, 2002 9:06 am
by twigletmac
What does the full code for the deletion part of edit.php currently look like?
Mac
Posted: Fri Nov 29, 2002 9:12 am
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";
}
Posted: Fri Nov 29, 2002 9:14 am
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
Posted: Fri Nov 29, 2002 9:20 am
by new to php
still doesn't work
Posted: Fri Nov 29, 2002 9:21 am
by twigletmac
Does it give you the same error? Have you done:
to check the SQL query?
Mac