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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Could someone point out an error on my script?

    Its probably just a simple noob error, (i am new to PHP) but the following code will execute fine but only send the email...

    PHP Code:
    <?php

    if (isset($_REQUEST['email']) && $_REQUEST['email'] != '') {
        
    $to "my@email.com";
        
    $subject "Contact Details Captured";
        
    $message "New message from: {$_REQUEST['fname']} {$_REQUEST['sname']} {$_REQUEST['email']}.\r\n\r\n {$_REQUEST['telno']}";
        
    $message .= $_REQUEST['message'];
        
        
    mail($to,$subject,$message);
        
    $dbserver "localhost";
        
    $dbuser "myusername";
        
    $dbpwd "mypassword";

        
    $dbcon mysql_connect("$dbserver""$dbuser""$dbpwd");

        
    mysql_select_db("test"$dbcon);

        
    $query "INSERT INTO captured_data (fname, sname, email, telno) VALUES ('$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[telno]')";

        
    mysql_query($query$dbcon); 

        
    mysql_close($dbcon);

        
        
    header('Location:http://www.myurl.co.uk/home');}
    else{
    header('Location:http://www.myurl.co.uk/error.php#forma');
    exit();
    }

    ?>
    and doing it the following way will add the data to my database but not send the email.

    PHP Code:
    <?php

    if (isset($_REQUEST['email']) && $_REQUEST['email'] != '') {
        
    $to "my@email";
        
    $subject "Contact Details Captured";
        
    $message "New message from: {$_REQUEST['fname']} {$_REQUEST['sname']} {$_REQUEST['email']}.\r\n\r\n {$_REQUEST['telno']}";
        
    $message .= $_REQUEST['message'];
        
        
        
    $dbserver "localhost";
        
    $dbuser "myusername";
        
    $dbpwd "mypassword";

        
    $dbcon mysql_connect("$dbserver""$dbuser""$dbpwd");

        
    mysql_select_db("test"$dbcon);

        
    $query "INSERT INTO captured_data (fname, sname, email, telno) VALUES ('$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[telno]')";

        
    mysql_query($query$dbcon); 

        
    mysql_close($dbcon);

        
    mail($to,$subject,$message);
        
    header('Location:http://www.myurl.co.uk/home');}
    else{
    header('Location:http://www.myurl.co.uk/error.php#forma');
    exit();
    }

    ?>
    As I said it is probably a basic misunderstanding of the PHP the syntax.

    Cheers, Chris.
    Last edited by Chris1989; 11-03-2010 at 11:19 AM.

  • #2
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Romania
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't understand why it doesn't work but can you try this code?
    it's not a big change it just verifies if mysql query completed successfully if it did then it emails the content after which it waits 2 seconds (I am thinking maybe you are redirecting the page before mail complets, although this should not be the case, but anyway...)
    PHP Code:
    <?php

    if (isset($_REQUEST['email']) && $_REQUEST['email'] != '') {
        
    $to "my@email";
        
    $subject "Contact Details Captured";
        
    $message "New message from: {$_REQUEST['fname']} {$_REQUEST['sname']} {$_REQUEST['email']}.\r\n\r\n {$_REQUEST['telno']}";
        
    $message .= $_REQUEST['message'];
        
        
        
    $dbserver "localhost";
        
    $dbuser "myusername";
        
    $dbpwd "mypassword";

        
    $dbcon mysql_connect("$dbserver""$dbuser""$dbpwd");

        
    mysql_select_db("test"$dbcon);

        
    $query "INSERT INTO captured_data (fname, sname, email, telno) VALUES ('$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[telno]')";

        if(
    mysql_query($query$dbcon)) 
        
    mail($to,$subject,$message);
        
    sleep(2);
        
    header('Location:http://www.myurl.co.uk/home');}
    else{
    header('Location:http://www.myurl.co.uk/error.php#forma');
    exit();
    }

    ?>
    I like to play jocuri cu masini and jocuri barbie

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Thanks for the reply, I did try it but still doesnt send the email it only adds the data to the database.

    It shouldnt redirect before sending the email as the header function is placed after the redirect indicating that it should send the email then redirect after adding the details to the database.

    Confusing.
    Last edited by Chris1989; 11-03-2010 at 04:18 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Do emails otherwise send on your server? Is this the first time you're having trouble with the mail function?

  • #5
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    I can't see any obvious errors, perhaps you could check the error log? Assuming display errors is off. Perhaps start it with error_reporting(E_ALL) too.

    Another thing: http://php.net/manual/en/function.my...ape-string.php

    Code:
    "INSERT INTO captured_data (fname, sname, email, telno) VALUES ('".mysql_real_escape_string($_POST[fname], $dbcon)."','".mysql_real_escape_string($_POST[sname], $dbcon)."','".mysql_real_escape_string($_POST[email], $dbcon)."','".mysql_real_escape_string($_POST[telno], $dbcon)."')";
    ... or read up about prepared statements.

    Database sanitisation is really important from a security and bug fixing point of view.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

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