Dear Friends,

I have a table in mysql database, which retains emails, I have PHP 
application where I enter emails and emails get stored in mysql database,table, I have 
an option to unsubscribe as well, which works, 


However, Problem application is giving me is in case I unsubscribe one email 
it deletes all emails from database. table. I have pasted php code and mysql 
dump. 
Any guidance, please.
------------------------------------------------------------------------------
------------
kemails.php page which i use to subscribe and unsubscribe
------------------------------------------------------------------------------
---------
<?php
//set up a couple of functions
function doDB() {
   global $conn;
   //connect to server and select database; you may need it
   $conn = mysql_connect("localhost", "", "") or die(mysql_error());
   mysql_select_db("b",$conn)  or die(mysql_error());
}

function emailChecker($email) {
   global $conn, $check_result;
   //check that email is not already in list
   $check = "select id from kemails where email = '$email'";
   $check_result = mysql_query($check,$conn) or die(mysql_error());
}

//determine if they need to see the form or not
if ($_POST[op] != "ds") {
   //they do, so create form block
   $display_block = "
   <form method=POST action=\"$_SERVER[PHP_SELF]\">

   <p><strong>kemails</strong><br>
   <input type=text name=\"email\" size=40 maxlength=150>

   <p><strong>Action:</strong><br>
   <input type=radio name=\"action\" value=\"sub\" checked> subscribe
   <input type=radio name=\"action\" value=\"unsub\"> unsubscribe

   <input type=\"hidden\" name=\"op\" value=\"ds\">

   <p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
   </form>";

} else if (($_POST[op] == "ds") && ($_POST[action] == "sub")) {
    //trying to subscribe; validate email address
   if ($_POST[email] == "") {
       header("Location: kemails.php");
       exit;
   }

   //connect to database
   doDB();

   //check that email is in list
   emailChecker($_POST[email]);

   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
        //add record
        $sql = "insert into kemails values('', '$_POST[email]')";
        $result = mysql_query($sql,$conn) or die(mysql_error());
        $display_block = "<P>Thanks for signing up!</P>";
   } else {
       //print failure message
       $display_block = "<P>You're already subscribed!</P>";
   }
} else if (($_POST[op] == "ds") && ($_POST[action] == "unsub")) {
   //trying to unsubscribe; validate email address
   if ($_POST[email] == "") {
       header("Location: kemails.php");
       exit;
   }

   //connect to database
   doDB();

   //check that email is in list
   emailChecker($_POST[email]);

   //get number of results and do action
   if (mysql_num_rows($check_result) < 1) {
       //print failure message
       $display_block = "<P>Couldn't find your address!</P>
       <P>No action was taken.</P>";
   } else {
       //unsubscribe the address
       $id = mysql_result($check_result, 0, "id");
       $sql = "delete from kemails where id = '$id'";
       $result = mysql_query($sql,$conn) or die(mysql_error());
       $display_block = "<P>You're unsubscribed!</p>";
   }
}
?>
<HTML>
<HEAD>
<TITLE>Subscribe/Unsubscribe K-EMAILS</TITLE>
</HEAD>
<BODY>
<h1>Subscribe/Unsubscribe J&K-EMAILS</h1>
<a href="kemails.php">Back</a>
<?php echo "$display_block"; ?>
</BODY>
</HTML>
------------------------------------------------------------------------------
-----------
kselect.php page which processes the subscribe and unsubscribe
------------------------------------------------------------------------------
--
<?php
// open the connection
$conn = mysql_connect("localhost", "", "");

// pick the database to use
mysql_select_db("b",$conn);

// create the SQL statement
$sql = "SELECT * FROM kemails";

// execute the SQL statement
$result = mysql_query($sql, $conn) or die(mysql_error());

//go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
 // give a name to the fields
    $id  = $newArray['id'];
    $email = $newArray['email'];
    //echo the results onscreen between ""can also //type$id is and &email is
    echo " $email <br>";
}
?>
------------------------------------------------------------------------------
--------------
mysql table structure
kemails

mysql> describe kemails
    -> ;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      |      | PRI | 0       |       |
| email | varchar(150) |      | PRI |         |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Reply via email to