www.webdeveloper.com
Results 1 to 10 of 10

Thread: PHP practise & exercises

  1. #1
    Join Date
    May 2007
    Location
    London, United Kingdom
    Posts
    430

    PHP practise & exercises

    I have been grafting hard with my PHP exercises and what not. Been going really well... But got stuck on a little hitch. This should work... Why doesn't it???

    PHP Code:
    //variables
    $from 'DO NOT REPLY - Elvis Online Store';
    $subject $_POST['subject'];
    $body $_POST['elvismail'];

    //validation for fields not to be empty
    if((!empty($subject)) && if(!empty($body)))
     {
    //connect to db
    $dbc mysqli_connect('localhost','root','PASSWORD') or die('Error connecting to MySQL');

    //choose the database
    mysqli_select_db($dbc'elvis_store');
                
    $query "SELECT * FROM email_list";
    $result mysqli_query($dbc$query);
    while(
    $row mysqli_fetch_array($result)) 
    {
    $firstName $row['first_name'];
    $lastName $row['last_name'];
    $email $row['email'];
                            
    $msg "Dear $firstName $lastName, \n $body";
                            
    mail('$email','$subject','$msg','$from');
    echo 
    "email has been sent to: $email<br />";
    }    
                    
    //close connection
    mysqli_close($dbc);
    }
                
    else
    {    
    echo 
    "You forgot the body and/or subject";

    The main point of this chapter is to create a "sticky" form, thus teaching if's and else's and operators. So, the end result should either be, a list of successfully sent emails, or a empty form field notification.

    PS: This does go into more detail in the book. This is just where I am stuck atm.

    TIA (thanks in advance)

  2. #2
    Join Date
    Oct 2009
    Posts
    9

    Thumbs up

    Here is the right Code.........



    Be careful while using select_db after the connect



    //variables
    $from = 'DO NOT REPLY - Elvis Online Store';
    $subject = $_POST['subject'];
    $body = $_POST['elvismail'];

    //validation for fields not to be empty
    if((!empty($subject)) && if(!empty($body)))
    {
    //connect to db
    $dbc = mysqli_connect('localhost','root','PASSWORD') or die('Error connecting to MySQL');

    //choose the database

    //HERE WAS THE MISTAKE !!!
    mysqli_select_db('elvis_store',$dbc);// you had reversed the arguments !!
    //or just do it mysqli_select_db('elvis_store') it will take the last successful //connection by default
    $query = "SELECT * FROM email_list";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result))
    {
    $firstName = $row['first_name'];
    $lastName = $row['last_name'];
    $email = $row['email'];

    $msg = "Dear $firstName $lastName, \n $body";

    mail('$email','$subject','$msg','$from');
    echo "email has been sent to: $email<br />";
    }

    //close connection
    mysqli_close($dbc);
    }

    else
    {
    echo "You forgot the body and/or subject";
    }



    //////Wanna find smile back on your Face buddy

  3. #3
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Umm ... I think your arguments were actually in the appropriate order. They're "backwards" from the "pre-improved" PHP mysql functions. Your problem may actually be here:
    PHP Code:
    $from 'DO NOT REPLY - Elvis Online Store';
      
    // ... snip ... 
    mail('$email','$subject','$msg','$from'); 
    I'm pretty sure the 4th parameter needs properly formatted mail headers. "DO NOT REPLY - Elvis Online Store" will probably not suffice. See the examples on the mail() doc page.

  4. #4
    Join Date
    Aug 2009
    Posts
    593
    Quote Originally Posted by Hooded_Villian View Post
    mail('$email','$subject','$msg','$from');
    Also don't forget to get rid of the single quotes around the arguments or change them to double quotes.

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Also don't forget to get rid of the single quotes around the arguments or change them to double quotes.
    Indeed ... sorry I missed that. Maybe I should go home for the day--missing out of basic syntactical things ...

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    Another thing to note - you are telling the user the mail was sent but you don't actually know this because you don't check the return from the mail() function. As svidgen points out, the mail would likely not have sent but your script would still inform the user that it had.

  7. #7
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    As svidgen points out, the mail would likely not have sent but your script would still inform the user that it had.
    I'm embarrassed. I really didn't even catch the syntax issues--that's what I meant to say in my last post. But, in my morning half-awake state I couldn't even communicate that properly.

    Sorry about that ...

    I'm glad some other folks were watching this thread. I would have possibly helped solved the smallest part of the problem, only to have the remaining, bigger, and more obvious issue(s) hanging out there ...

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Sometimes the easy bugs are the hardest to see! I looked straight past the quotes too.

  9. #9
    Join Date
    May 2007
    Location
    London, United Kingdom
    Posts
    430
    hahaha... U guys crack me up. I got the whole thind running awhile ago. Can't remember how. What I have resorted to doing is using comments, for scripts that don't work. I will comment out everything that could potentially be a problem area. The uncomment stage for stage, until a problem occurs and then I can pin point it... Thanks alot for all the help and input.

    Question: How would I confirm that the email was sent???

  10. #10
    Join Date
    Nov 2008
    Posts
    2,477
    Mail yourself then check your inbox :P You can check the return of the mail() function, but that only tells you if the mail was accepted for delivery, not that it was actually sent and received properly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles