Trouble with posting data into MySql table - table remains empty

83 views Asked by At

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.

2

There are 2 answers

4
DMortensen On

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:

<?php
if(isset($_POST['firstname']){
    add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']);
}
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;
}

?>
0
Justin On

First:
If this is all your code to add a new user, this is not a good way to do it. I suggest you change the logic that handles adding a new user to your table for more consistent and related applications for future.
Second
Like @Robert suggests in his comment, you need to avoid using deprecated functions to save time and better handling.
Third:
Test your SQL code in PhpMyAdmin if available so you can see if it is something related to connection/ coding problems.
Forth:
Try to tweek your code and test it if it might work.

// you can try this:

    $sql= "INSERT INTO 'users' ('firstname', 'lastname', 'email') VALUES ('".$firstname."','".$lastname."','".$email."')";
     $result = mysql_query($sql);

Fifth:
Make sure your add_user(); function is being called properly, in the right place-after the variables defined-. Hope this helps.