I'm new to PHP and MySql and I've been trying to solve this for hours with no avail. I'm trying to create a mailing list. After filling out the forms and hitting submit, the data will just not appear in the database table. Any help is appreciated. Don't really know how to debug yet, so I don't get an error either. I've been following this tutorial http://www.youtube.com/watch?v=HL884ugSL8c
mail.inc.php file:
<?php
// adds information to the table
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 table
function remove_user($email){
$email = mysql_real_escape_string($email);
mysql_query("DELETE FROM 'users' WHERE 'email' = '{$email}'");
}
// sends a given message to all subscribed users
function mail_all($subject, $message, $headers){
$users = mysql_query("SELECT 'firstname', 'email' FROM 'users'");
while (($user = mysql_fetch_assoc($users)) !== false){
$body = "Hi, {$user['firstname']}\n\n{$message}\n\nUnsubscribe: ";
mail($user['email'], $subject, $body, $headers);
}
}
?>
signup.php file:
<?php
include('core/init.inc.php');
// checks if all forms have been filled !!!!!!!
if (isset($_POST['firstname]'], $_POST['lastname'], $_POST['email'])){
$errors = array();
if (preg_match('/^[a-z]+$/i', $_POST['firstname']) === 0){
$errors[] = 'Your first name should contain letters only.';
}
if (preg_match('/^[a-z]+$/i', $_POST['lastname']) === 0){
$errors[] = 'Your last name should contain letters only.';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'This email address does not appear to be valid';
}
if (empty($errors)){
add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']);
}
}
?>
<form action="" method="post">
<p>
<label for="firstname">Firstname</label>
<input type="text" name="firstname" id="firstname" />
</p>
<p>
<label for="lastname">Lastname</label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
<label for="email">E-mail</label>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" value="Signup" />
</p>
</form>
The init.inc.php file contains this:
<?php
mysql_connect('localhost','wd','wd'); //not bothered
mysql_select_db('leedsattractions');
$path = dirname(__FILE__);
include("{$path}/inc/mail.inc.php");
?>
And I've added the rest of the mail.inc.php file to the first embed upstairs.
Where is your function add_user() being called? You need to make sure it's called. I would suggest something like this to call it if the form is submitted: