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.

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

Listing 30.14 Continued<br />

return false;<br />

}<br />

$query = "select count(*) from lists where listname = '".$details['name']."'";<br />

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

$row = $result->fetch_array();<br />

if($row[0] > 0) {<br />

echo "Sorry, there is already a list with this name.";<br />

return false;<br />

}<br />

$query = "insert into lists values (NULL,<br />

'".$details['name']."',<br />

'".$details['blurb']."')";<br />

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

return $result;<br />

}<br />

}<br />

This function performs a few validation checks before writing to the database: It checks<br />

that all the details were supplied, that the current user is an administrator, <strong>and</strong> that the<br />

list name is unique. If all goes well, the list is added to the lists table in the database.<br />

Uploading a New Newsletter<br />

Finally, we come to the main thrust of this application: uploading <strong>and</strong> sending newsletters<br />

to mailing lists.<br />

When an administrator clicks on the Create Mail button, the create-mail action is<br />

activated, as follows:<br />

case 'create-mail':<br />

display_mail_form(get_email());<br />

break;<br />

The administrator then sees the form shown in Figure 30.12.

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

Saved successfully!

Ooh no, something went wrong!