php email info from database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

php email info from database?

Post by newtoophp »

Hi all...

I have a simple php email script which collects the email address and subject from a form.

When the form is submitted, I want an email to be sent to the address supplied by the user as well as a text field that is in my database.

I have everything working but I am not sure how to reference the actual database cell with the info.
I want the variable $message to come from my database, I already have the connections to the database and table working.
When I submit, the script sends the email correctly minus the $message from the database. ( thats what i need help with)

Code: Select all

/*                           
$email = $HTTP_POST_VARS['email'];
$subject= $HTTP_POST_VARS['subject'];
$message = HOW DO I WRITE THIS TO GET MY DATA FROM THE DATABASE?
 
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see  PHP manual for details. */
 
                                if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
                                  echo "<h4>Invalid email address</h4>";
                                  echo "<a href='javascript&#058;history.back(1);'>Back</a>";
                                } elseif ($subject == "") {
                                  echo "<h4>No subject</h4>";
                                  echo "<a href='javascript&#058;history.back(1);'>Back</a>";
                                }
                                
                                /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
                                elseif (mail($email,$subject,$message)) {
                                  echo "<h4>Thank you for sending email</h4>";
                                } else {
                                  echo "<h4>Can't send email to $email</h4>";
                                }
Any help or examples would be greatly appreciated,

Thanks
jfeaz
Forum Newbie
Posts: 7
Joined: Sat Sep 06, 2008 7:46 pm

Re: php email info from database?

Post by jfeaz »

I think what you need to do is define $message with a MySQL query:

Code: Select all

 
$query="SELECT FieldWhereMessageIs FROM TableWhereMessageIs WHERE YourTest = YourCondition";
$message = mysql_query($query);
 
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

Re: php email info from database?

Post by newtoophp »

I have tried several variations of the $query idea, but still no success.

Basically what I am trying to do is make my email $message in the below code:

Code: Select all

elseif (mail($email,$subject,$message)) {
Be taken from a particular row and column of my database.
(Not sure if my first post made this clear.)

I can still submit and email with my form, but I cannot get the $message to be pulled from the database :oops:

Is it even possible to make the $message something other than what someone has input into a textbox? Or am I wasting my time?

Thanks again
jfeaz
Forum Newbie
Posts: 7
Joined: Sat Sep 06, 2008 7:46 pm

Re: php email info from database?

Post by jfeaz »

When you say you've tried a query, what is the specific result?
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

Re: php email info from database?

Post by newtoophp »

When I added the query to the code, I could still submit the form and the email would send. It just would have a nothing in the message body. I did not even get an error message.

So no real results, do I need to add something more to the query?
jfeaz
Forum Newbie
Posts: 7
Joined: Sat Sep 06, 2008 7:46 pm

Re: php email info from database?

Post by jfeaz »

Well, the first thing to check is to make sure that you are actually connected to the database. If this script is not an include within a larger file, like index.php, that also contains or includes your login script, you aren't connected to the database, even if a different file that was accessed sooner contains the login.
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

Re: php email info from database?

Post by newtoophp »

Yes, it is connected to my database. The code is below and it returns the echos' correctly,

Code: Select all

mysql_connect("localhost", "User", "Password") or die(mysql_error());   
                      echo "<font color='white' face='arial'><span style='font-size: 10px;'> Connection to the  Server...<br/>";    
                      mysql_select_db("The_Database") or die(mysql_error());   
                      echo "Database Active!<br/></span></font>";
So I am pretty sure everything is connected...

I think the problem is when I do my $query I must be using the incorrect method, because I have just started php.

So do you think it is possible to have the $message be something from a database as opposed to something that a user submits with a form?
broch
Forum Newbie
Posts: 4
Joined: Sat Sep 13, 2008 7:38 am

Re: php email info from database?

Post by broch »

I think what you need to do is:


$sql = 'SELECT * FROM your_database_table WHERE ID='.$dataid;
$result = mysql_query($sql);

$row = mysql_fetch_array($result);

//you can then access your data with $row['column name']

to see what you get you can do a:

print_r($row); //will print the whole array as text

broch
Post Reply