Hi
I have a mysql database table, for which I wish to allow my users to add, edit and delete their record.
Add and edit are ok, but when the user tries to delete their record, they should be prompted by a confirmation box. If they press OK, then it should go ahead and delete, however, if they press cancel, it should not delete, but return them to the home page.
Currently, the OK button works fine, but if the user presses cancel, they are re-directed to the home page, but the record is still deleted.
Here is the code:
<?php
$database = "Istanbul";
$host="localhost";
$link = mysql_connect($host)or die ("Couldn't connect");
mysql_select_db($database,
$link) or die ("Couldn't select database");
?>
<html>
<head>
<title>Unsubscribe</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<style type="text/css">
<!--
-->
</style>
<link href="Tan.css" rel="stylesheet" type="text/css">
</head>
</head>
<body bgcolor="#FFFF66">
<?
if (!$_POST['unsubscribe']) {
?>
<center>
<form method="post" action="" name="theForm">
<table border=1 cellpadding=10 cellspacing=0 bgcolor = white>
<tr>
<td>
<font size=+1><b>Unsubscribe</b>
</font>
</td>
</tr>
<tr>
<td>
Enter email: <input name="email" type="text" value="">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" name="unsubscribe" value="Unsubscribe" >
</td>
</tr>
</table>
</form>
</center>
<?php
}
if ($_POST['unsubscribe'])
{
$email1 = trim($_POST['email']);
if ( empty($email1))
{
$error = true; // input validation failed
echo "<span style=\"color:red\">Error!
1 Please enter a valid email address.</span><br>\n";
?>
<p>Click <a href="javascript:history.g
o(-1)">her
e</a> to go back.</p>
<?
}
else if (!empty($email1))
{
$sql = "SELECT email FROM mailinglist WHERE email = '$email1'";
$result = mysql_query($sql, $link) or die ("Couldn't execute query");
while($a_row=mysql_fetch_r
ow($result
))
{
foreach($a_row as $field)
$user = "$field";
}
if ($user)
{
?>
<script type="text/javascript">
var answer = confirm ("Are you sure you want to unsubscribe?")
if (answer==false)
window.location="mainFrame
.htm"
else
</script>
<?php
$query = "DELETE FROM mailinglist WHERE email LIKE '".$email1."' LIMIT 1";
$result = mysql_query($query, $link) or die ("Couldn't add the data");
if ($result)
{
echo "Success";
}
else
{
echo "<span style=\"color:red\">Error!
2 Please enter a valid email address.</span><br>\n";
?>
<p>Click <a href="javascript:history.g
o(-1)">her
e</a> to go back.</p>
<?
}
}
else if (!$user)
{
echo "<span style=\"color:red\">Error!
3 Please enter a valid email address.</span><br>\n";
?>
<p>Click <a href="javascript:history.g
o(-1)">her
e</a> to go back.</p>
<?
}
}
} // End if post ok conditional
?>
</body>
</html>
Thank you for looking!