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 ?>