I am trying to create an email form that allows the user to subscribe or unsubscribe to an email list. It goes bad at line 25. This is what I have so far:

I can not figure out how to write the unsubscribe part. I used if/elseif/else:

1 <?php
2 require_once 'include/Smarty/libs/Smarty.class.php';
3 require_once 'include/Database.inc.php';
4
5 $smarty = new Smarty();
6
7 define ('TEMPLATE', 'manage.html');
8
9 $email = $_POST['email'];
10
11 // check email against db
12 if (!empty($_POST['email']))
13 {
14 $query = 'SELECT id
15 FROM subscribers
16 WHERE email = \''.$_POST['email'].'\'';
17 $res = $db->query($query);
18
19 // do they exist? yes
20 if ($res->numRows() > 0)
21 {
22 $message = 'Our records indicate that you are subscribed.';
23 }
24 // unsubscribe
25 elseif ($res->email == $unsub) //this is where I go wrong!!!!!
26 {
27 $query = 'DELETE FROM subscribers
28 WHERE email = \''.$_POST['email'].'\'';
29 $message = 'You have been unsubscribed';
30 }
31 else {
32 // they don't exist, enter new record
33 $query = 'INSERT INTO subscribers
34 SET email = \''.$_POST['email'].'\'';
35 $message = 'Thank You!';
36 }
37 }
38
39
40 $smarty->assign(array(
41 'title' => 'E-Mail Form',
42 'message' => $message
43 ));
44
45
46 $smarty->display(TEMPLATE);
47 ?>

Your (CODE) statements are showing in the post so something wasn't done correctly in posting this. It shows 2 code statements at the beginning and a (/code) at the end. Bottom line is that it isn't being taken as code and the option to view plain text isn't there. That limits our ability to help you.

see below

<?php
 require_once 'include/Smarty/libs/Smarty.class.php';
 require_once 'include/Database.inc.php';

 $smarty = new Smarty();

 define ('TEMPLATE', 'manage.html');

 $email = $_POST['email'];

 // check email against db
 if (!empty($_POST['email']))
 {
 $query = 'SELECT id
 FROM subscribers
 WHERE email = \''.$_POST['email'].'\'';
 $res = $db->query($query);

 // do they exist? yes
 if ($res->numRows() > 0)
 {
 $message = 'Our records indicate that you are subscribed.';
 }
 // unsubscribe
 elseif ($res->email == $unsub) //this is where I go wrong!!!!!
 {
 $query = 'DELETE FROM subscribers
 WHERE email = \''.$_POST['email'].'\'';
 $message = 'You have been unsubscribed';
 }
 else {
 // they don't exist, enter new record
 $query = 'INSERT INTO subscribers
 SET email = \''.$_POST['email'].'\'';
 $message = 'Thank You!';
 }
 }


 $smarty->assign(array(
 'title' => 'E-Mail Form',
 'message' => $message
 ));


 $smarty->display(TEMPLATE);
 ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.