Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Having trouble writing to database for a mailing list

    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) ? truefalse;
    }

    //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.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    you are using quotes instead of backticks in your query, so 'users' should be `users` or just users etc, backticks are optional (you will probably be told) non the less I find them useful.
    PHP Code:
    <?php
    $result 
    mysql_query("INSERT INTO `users` (`firstname`,`lastname`,`email`) VALUES ('{$firstname}','{$lastname}','{$email}')");
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    constantgamer24 (05-23-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    you are using quotes instead of backticks in your query, so 'users' should be `users` or just users etc, backticks are optional (you will probably be told) non the less I find them useful.
    PHP Code:
    <?php
    $result 
    mysql_query("INSERT INTO `users` (`firstname`,`lastname`,`email`) VALUES ('{$firstname}','{$lastname}','{$email}')");
    ?>

    Other than that, check your form inputs to make sure they have the correct names I guess.[/quote]

    Thank you so much!

    I thought it was just a different font. Didn't realise that the ` was on the same key with the ~ so I just assumed they were 'singleQuotes'

    Anyway I changed it and it now works.

    Thank you again


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •