So I am trying to set up a mailing list for my website; following this tutorial
http://www.youtube.com/watch?v=dQBFBuYyHVk
I've gotten a bit stuck. No matter what I do I can't get it to write to the database. I think it might be a MySQL problem, but I think it also might be a problem with a php array.
So just like the video I've set up the file dirrectories the same
/mailing_list {
error_log
sendmail.php
signup.php
unsubscribe.php
core(folder)
}
/mailing_list/core{
init.inc.php
inc(folder)
}
/mailing_list/core/inc{
mail.inc.php
}
So the only files that really matter right now are the signup.php, the init.inc.php and the mail.inc.php files as at this point I'm stuck on inserting into the Database.
signup.php
PHP Code:
<?php
include('core/init.inc.php');
if (isset($_POST['firstname'], $_POST['lastname'], $_POST['email'])){
$errors = array();
if (preg_match('/^[a-z]+$/i', $_POST['firstname'] === 0)){
$errors[] = 'Your first name should only be comprised of letters';
}
if (preg_match('/^[a-z]+$/i', $_POST['lastname'] === 0)){
$errors[] = 'Your last name should only be comprised of letters';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'Your email address is not a valid format';
}
if (empty($errors)){
add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-strick.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OpenKonga</title>
<link rel="stylesheet" type="text/css" href="stylez.css" media="all" />
</head>
<body>
<div>
<?php
if(empty($errors) === false){
echo '<ul><li>', implode('</li><li>',$errors), '</li><ul>';
}else if(isset($errors) === false){
echo 'Fill in the form to subscribe to our mailing list, so you can be the first to know!';
}else{
echo 'You have been added to our mailing list; expect to hear from us soon!';
}
?>
</div>
<div>
<form action="" method="post">
<p>
<label for="firstname">First Name: </label>
<input type="text" name="firstname" id="firstname" />
</p>
<p>
<label for="lastname">Last Name: </label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
<label for="eMail">eMail Address:</label>
<input type="text" name="eMail" id="eMail" />
</p>
<p>
<input type="submit" value="Signup" />
</p>
</form>
</div>
</body>
</html>
init.inc.php
PHP Code:
<?php
mysql_connect('localhost','USERNAME','PASSWORD')
or die("Error1");
mysql_select_db('openkong_mailing_list')
or die("Error2");
$path = dirname(__FILE__);
include("{$path}/inc/mail.inc.php");
?>
mail.inc.php
PHP Code:
<?php
//adds the given email address to the subscribers table in the mailingList database
function add_user($firstname, $lastname, $email){
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$email = mysql_real_escape_string($email);
$result = mysql_query("INSERT INTO 'users' ('firstname','lastname','email') VALUES ('{$firstname}','{$lastname}','{$email}')");
return ($result !== false) ? true: false;
}
//removes the given email address from the subscribes table in the mailingList database
function remove_user($email){
$email = mysql_real_escape_string($email);
mysql_query("DELETE FROM 'users' WHERE 'email' = '{$email}'");
}
//sends the message you create to all eMail addresses
function mail_all($subject, $message, $headers){
$users = mysql_query("SELECT 'firstname','eMail' FROM 'users'");
while (($user = mysql_fetch_asoc($users)) !== false){
$body = "Greetings, {$user['firstname']}!!!\n\n{$message}\n\nUnsubscribe: ";
mail($user['email'], $subject, $body, $headers);
}
}
?>
I think my insert statement is fine
Code:
$result = mysql_query("INSERT INTO 'users' ('firstname','lastname','email') VALUES ('{$firstname}','{$lastname}','{$email}')");
in the init.inc.php file I added the or die() statements to help debug
PHP Code:
mysql_select_db('mailing_list')
or die("Error2");
was returning "Error2"
but I changed it to
PHP Code:
mysql_select_db('openkong_mailing_list')
or die("Error2");
and it works. Not sure why I need the openkong there, but either way that was not preventing it from inserting into the database as it still is not working
What I don't think is working is the
$errors = array(); as when I try
print_r($errors); It does not print anything.
Does anybody have an idea of what to try/how to go about debugging this?
Thanks in advance.