SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding an auto-increment ID from database to a form email reply

    I have created a form that collects information from the web site visitor. All fields are passing to the database and being reported in an email to the visitor. This is all occurring in the same processor script. I would like to add the auto-increment id generated when the data is inserted to the database back to the email sent to the visitor confirming all data received. I have tried to use

    echo "ID of last inserted record is: " . mysql_insert_id();

    and some variations, but with no success. I realize that I am most likely not getting the 'id' from the right place, as it is the only data that has to be queried from the database. Everything else is just passing through the script to the email process. Thanks for your assistance.

    I have had to remove the code to remove a link that is keeping me from posting. Oh well.

    The processor is typical.

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    I have created a form that collects information from the web site visitor. All fields are passing to the database and being reported in an email to the visitor. This is all occurring in the same processor script. I would like to add the auto-increment id generated when the data is inserted to the database back to the email sent to the visitor confirming all data received. I have tried to use

    echo "ID of last inserted record is: " . mysql_insert_id();

    and some variations, but with no success. I realize that I am most likely not getting the 'id' from the right place, as it is the only data that has to be queried from the database. Everything else is just passing through the script to the email process. Thanks for your assistance.

    I have had to remove the code to remove a link that is keeping me from posting. Oh well.

    The processor is typical.
    can you try to post your code without the link in it?
    because in theory the mysql_insert_id() is the correct function to retrieve the last inserted id. So maybe some other line is wrong in your code.

    edit
    Maybe also possible errors?
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    can you try to post your code without the link in it?
    because in theory the mysql_insert_id() is the correct function to retrieve the last inserted id. So maybe some other line is wrong in your code.

    edit
    Maybe also possible errors?
    Here is the last half of the code. The part that processes the email. I will attempt to post the first half once I figure out where the link is. The second to the last $message insert is the one where the ID would presumably be called to.




    <?php
    $to = $_POST['field_3'];
    $subject = "Here's your ID!";
    $headers .= "From: Us to You \r\n";
    $headers .= "BCc: email\r\n";
    $headers .= "Return-Path: email\r\n";
    $headers .= "Reply-To: email\r\n";
    $message = "Here's your ID! Keep it in a safe place\n\n";
    $message .= "First Name: " . $_POST['field_1'] . "\n";
    $message .= "Last Name: " . $_POST['field_2'] . "\n";
    $message .= "Email: " . $_POST['field_3'] . "\n";
    $message .= "City: " . $_POST['field_4'] . "\n";
    $message .= "State: " . $_POST['field_5'] . "\n";
    $message .= "Country: " . $_POST['field_6'] . "\n";
    $message .= "ID: " . $_POST['id'] . "\n";
    $message .= "Thank you!\n\n";
    if (mail($to,$subject,$message,$headers)) {
    echo "<p><font color=white face=verdana size=3>Thanks for registering!</font>";
    } else {
    echo "<p><font color=white face=verdana size=3>email could not be sent</font>";
    }
    ?>[/color]

  4. #4
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    Here is the last half of the code. The part that processes the email. I will attempt to post the first half once I figure out where the link is. The second to the last $message insert is the one where the ID would presumably be called to.



    PHP Code:
    <?php 
     $to 
    $_POST['field_3']; 
     
    $subject "Here's your ID!"
     
    $headers .= "From: Us to You \r\n"
     
    $headers .= "BCc: email\r\n"
     
    $headers .= "Return-Path: email\r\n"
     
    $headers .= "Reply-To: email\r\n"
     
    $message "Here's your ID! Keep it in a safe place\n\n"
     
    $message .= "First Name: " $_POST['field_1'] . "\n"
     
    $message .= "Last Name: " $_POST['field_2'] . "\n"
     
    $message .= "Email: " $_POST['field_3'] . "\n"
     
    $message .= "City: " $_POST['field_4'] . "\n"
     
    $message .= "State: " $_POST['field_5'] . "\n"
     
    $message .= "Country: " $_POST['field_6'] . "\n";
     
    $message .= "ID: " $_POST['id'] . "\n";
     
    $message .= "Thank you!\n\n"
     if (
    mail($to,$subject,$message,$headers)) { 
     echo 
    "<p><font color=white face=verdana size=3>Thanks for registering!</font>"
     } else { 
     echo 
    "<p><font color=white face=verdana size=3>email could not be sent</font>"

    ?>
    I can see that you are emailing the following variable as ID to your contacts:
    $_POST['id']

    Strange that you are using the $_POST array, because that is the array where the info of the user is located which he sends.... it is impossible that the last insert id is located in there.

    Somewhere in your script after you queried the db there should be something like:

    PHP Code:
    $userId =  mysql_insert_id(); 
    and then in your email it should be:

    PHP Code:
     $message .= "ID: " $userId "\n"
    Can you see where I'm getting at?
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    I can see that you are emailing the following variable as ID to your contacts:
    $_POST['id']

    Strange that you are using the $_POST array, because that is the array where the info of the user is located which he sends.... it is impossible that the last insert id is located in there.

    Somewhere in your script after you queried the db there should be something like:

    PHP Code:
    $userId =  mysql_insert_id(); 
    and then in your email it should be:

    PHP Code:
     $message .= "ID: " $userId "\n"
    Can you see where I'm getting at?
    I got it! Let me try it out. Thanks.

  6. #6
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is returning a value of '0'. I placed the $userId string just after the query insert for the 6 fields. And the $message string in the obvious place.

  7. #7
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can the id be called from a line that is being simultaneously inserted to?

  8. #8
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    It is returning a value of '0'. I placed the $userId string just after the query insert for the 6 fields. And the $message string in the obvious place.
    Quote Originally Posted by coastalina View Post
    Can the id be called from a line that is being simultaneously inserted to?

    A value of 0 being returned means that there has not happend an auto increment anywhere. the mysql_insert_id remembers the last value of auto_increment. And that value can never be 0, smallest is 1. So there is something wrong with the query you use to insert the data, or you don't have auto_increment set at all.

    do the values really get inserted in your database? have you checked?
    because the email just sends you the $_POST array values of the users, not the values retrieved from the DB.

    That other part of your code might be needed too.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  9. #9
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    A value of 0 being returned means that there has not happend an auto increment anywhere. the mysql_insert_id remembers the last value of auto_increment. And that value can never be 0, smallest is 1. So there is something wrong with the query you use to insert the data, or you don't have auto_increment set at all.

    do the values really get inserted in your database? have you checked?
    because the email just sends you the $_POST array values of the users, not the values retrieved from the DB.

    That other part of your code might be needed too.
    Auto_increment is set on the id field. I am getting my test submissions recorded.


    Here is the insert code:

    <?php

    $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

    session_start();
    if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) {
    include("config.inc.php");
    $link = mysql_connect($db_host,$db_user,$db_pass);
    if(!$link) die ('Could not connect to database: '.mysql_error());
    mysql_select_db($db_name,$link);
    $query = "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4,field_5,field_6) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . $_POST['field_3'] . "','" . $_POST['field_4'] . "','" . $_POST['field_5'] . "','" . $_POST['field_6'] . "')"; $userId = mysql_insert_id();




    include("confirm.html");
    }
    else {
    echo "Invalid Captcha String.";
    }

    ?>

  10. #10
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    Auto_increment is set on the id field. I am getting my test submissions recorded.


    Here is the insert code:

    <?php

    $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

    session_start();
    if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) {
    include("config.inc.php");
    $link = mysql_connect($db_host,$db_user,$db_pass);
    if(!$link) die ('Could not connect to database: '.mysql_error());
    mysql_select_db($db_name,$link);
    $query = "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4,field_5,field_6) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . $_POST['field_3'] . "','" . $_POST['field_4'] . "','" . $_POST['field_5'] . "','" . $_POST['field_6'] . "')"; $userId = mysql_insert_id();




    include("confirm.html");
    }
    else {
    echo "Invalid Captcha String.";
    }

    ?>
    In this code your query isn't getting executed.
    you have to use mysql_query before using the mysql_insert_id.

    PHP Code:


    <?php

    $where_form_is
    ="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

    session_start();
    if( (
    $_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { 
    include(
    "config.inc.php");
    $link mysql_connect($db_host,$db_user,$db_pass);
    if(!
    $link) die ('Could not connect to database: '.mysql_error());
    mysql_select_db($db_name,$link);
    $query "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4,field_5,field_6) VALUES ('" $_POST['field_1'] . "','" $_POST['field_2'] . "','" $_POST['field_3'] . "','" $_POST['field_4'] . "','" $_POST['field_5'] . "','" $_POST['field_6'] . "')";

    /* Look here */

    $result mysql_query($query$link);
    if (
    $result) {
      
    $userId =  mysql_insert_id();
    } else {
     
    /* Error in inserting */
    }



    include(
    "confirm.html");
    }
    else {
    echo 
    "Invalid Captcha String.";
    }

    ?>
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  11. #11
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    In this code your query isn't getting executed.
    you have to use mysql_query before using the mysql_insert_id.

    PHP Code:


    <?php

    $where_form_is
    ="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

    session_start();
    if( (
    $_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { 
    include(
    "config.inc.php");
    $link mysql_connect($db_host,$db_user,$db_pass);
    if(!
    $link) die ('Could not connect to database: '.mysql_error());
    mysql_select_db($db_name,$link);
    $query "INSERT into `".$db_table."` (field_1,field_2,field_3,field_4,field_5,field_6) VALUES ('" $_POST['field_1'] . "','" $_POST['field_2'] . "','" $_POST['field_3'] . "','" $_POST['field_4'] . "','" $_POST['field_5'] . "','" $_POST['field_6'] . "')";

    /* Look here */

    $result mysql_query($query$link);
    $userId =  mysql_insert_id();




    include(
    "confirm.html");
    }
    else {
    echo 
    "Invalid Captcha String.";
    }

    ?>


    Works like a champ! Thank you very much. You are a blessing to the rest of us out here. Still learning to write GOOD code.

  12. #12
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    Works like a champ! Thank you very much. You are a blessing to the rest of us out here. Still learning to write GOOD code.
    You are welcome
    I'm still learning too. There are much more knowledgeable gurus roaming the forums. But it feels good to be appreciated
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  13. #13
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in touch.

  14. #14
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It appears that the data is being inserted twice. It is only reporting back one line in the email, but is recording the first line and the next increment. Not a big problem, but will require some regular maintenance on the database. Any ideas?

  15. #15
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    It appears that the data is being inserted twice. It is only reporting back one line in the email, but is recording the first line and the next increment. Not a big problem, but will require some regular maintenance on the database. Any ideas?
    Can you check your code for another mysql_query($query) function?
    If the data gets inserted twice, then the query must be executed twice.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  16. #16
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is another query for the insert. Not sure how to set up the insert and the request for the id. I placed $userId = mysql_insert_id(); in that query the first time, I thought.

  17. #17
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coastalina View Post
    There is another query for the insert. Not sure how to set up the insert and the request for the id. I placed $userId = mysql_insert_id(); in that query the first time, I thought.
    I don't know how the other query is issued. It might pass some validations or something. So it is better to remove the mysql_query line that I said to add.
    Then move the mysql_insert_id() after the remaining mysql_query command.

    I hope it's clear what I'm saying.
    Also make sure that your email gets send after the query.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


Bookmarks

Posting Permissions

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