13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

718 Chapter 30 Building a Mailing List Manager<br />

Listing 30.12 subscribe()<strong>and</strong> unsubscribe() Functions from mlm_fns.php—<br />

These Functions Add <strong>and</strong> Remove Subscriptions for a User<br />

// subscribe this email address to this list<br />

function subscribe($email, $listid) {<br />

if((!$email) || (!$listid) || (!list_exists($listid))<br />

|| (!subscriber_exists($email))) {<br />

return false;<br />

}<br />

//if already subscribed exit<br />

if(subscribed($email, $listid)) {<br />

return false;<br />

}<br />

if(!($conn=db_connect())) {<br />

return false;<br />

}<br />

$query = "insert into sub_lists values ('".$email."', $listid)";<br />

$result = $conn->query($query);<br />

return $result;<br />

}<br />

// unsubscribe this email address from this list<br />

function unsubscribe($email, $listid) {<br />

if ((!$email) || (!$listid)) {<br />

return false;<br />

}<br />

if(!($conn=db_connect())) {<br />

return false;<br />

}<br />

$query = "delete from sub_lists where email = '".$email."' <strong>and</strong><br />

listid = '".$listid."'";<br />

$result = $conn->query($query);<br />

return $result;<br />

}<br />

The subscribe() function adds a row to the sub_lists table corresponding to the<br />

subscription; the unsubscribe() function deletes this row.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!