Page 1 of 1

General mailing list question

Posted: Mon Aug 29, 2005 11:13 am
by influx
I am incorporating a mailing list feature for my blog users and I was wondering what I need to fix in the database query.

I have a page where my users can input up to 10 email addresses with 10 accompanying names. Everything in this code works fine except for the last two values I want inputted (SESSIONemail and SESSIONname).

There has to be something wrong because it is always inputting NULL instead of the name or the email.

Here is the code:

Code: Select all

for($k=1;$k<=10;$k++)
{
  if($_SESSION['email_'.$k])
  {
    $query = "INSERT INTO mail_list (m_id, m_username, m_email, m_name) VALUES ('$user_id','{$_SESSION['username']}', '{$_SESSION['email_{$k}']}', '{$_SESSION['name_{$k}']}')";
    mysql_query($query) or die(mysql_error());
  }
}
Thanks!

Posted: Mon Aug 29, 2005 11:15 am
by John Cartwright
try a

Code: Select all

echo $query;
to see what's being inputted :wink:

Posted: Mon Aug 29, 2005 11:21 am
by influx
here is what it outputted:

INSERT INTO mail_list (m_id, m_username, m_email, m_name) VALUES ('862','johndoe', '', '')
INSERT INTO mail_list (m_id, m_username, m_email, m_name) VALUES ('862','johndoe', '', '')

Meaning, even though I inputted the last two things that should be inputted (SESSIONemail and SESSIONname), they still aren't being recognized...there is a problem with {$_SESSION['email_{$k}']} and {$_SESSION['name_{$k}']}...I just don't know how to fix it!

I'm sure I'm close but I've tried everything

Posted: Mon Aug 29, 2005 11:30 am
by feyd
I'd suggest storing those variables in temporary ones so you can simply reference those instead of dealing with this stuff.

Posted: Mon Aug 29, 2005 11:32 am
by influx
Interesting...I'm just thinking of it as the most effecient way for the server I'm running.

You really think it's better to store it in temp variables? Why?

Posted: Mon Aug 29, 2005 11:34 am
by feyd
for one, it's a bit easier for the compiler to translate, likely.. it's easier to read and understand quickly what's going on.

Posted: Mon Aug 29, 2005 11:36 am
by influx
Throughout building my site, I've been pretty careful to keep my code as short as possible. Is it really important to do this from my standpoint, you think?

Posted: Mon Aug 29, 2005 11:41 am
by feyd
code being short as possible doesn't necessarily make the code run faster. There are many examples around where one code is far shorter than another doing the exact same thing, but the longer one's approach is far faster.

A recent illustration: viewtopic.php?t=36790