Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-21-2012, 09:22 PM   PM User | #1
constantgamer24
New to the CF scene

 
Join Date: May 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
constantgamer24 is an unknown quantity at this point
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.
constantgamer24 is offline   Reply With Quote
Old 05-22-2012, 06:57 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,942
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Users who have thanked firepages for this post:
constantgamer24 (05-23-2012)
Old 05-23-2012, 01:52 AM   PM User | #3
constantgamer24
New to the CF scene

 
Join Date: May 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
constantgamer24 is an unknown quantity at this point
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
constantgamer24 is offline   Reply With Quote
Reply

Bookmarks

Tags
database, mailing list, mysql, php, writing to database

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:21 PM.


Advertisement
Log in to turn off these ads.