Displaying Auto Int from db?

Discussion in 'PHP' started by Kayz, Dec 11, 2008.

  1. #1
    Hi i am having trouble with something very small... i cannot display the auto id in the database via php.

    The following script works, it sends out the users details to their email address as they register. However at the bottom of the code you will notice something called "Ticket ID" this is to display the auto integer from the database.

    The whole script works i dont want to tweak anything else other than to display the ticket id, so when the person registers they are emailed the "auto integer" from the database as their ticket id.

    Any help would be much appreciated. I think i need to declare it but after so many tries it does not work.

    Cheers and many thanks in advance.

    <?php
    
    if(isset($submit)):
    
    include "config.php";
    
    
    
    endif;
    
    $firstname = $_POST['firstname'];
    $email = $_POST['email'];    
    $date = date("D M j Y G:i:s");
    
    if((!$firstname) || (!$surname) || (!$email) || (!$date)){
       include 'badlogin.php';
       exit();
    }
    
    
    // lets check to see if the email already exists
    
    $checkemail = mysql_query("SELECT email FROM ticket_registration WHERE email='$email'"); 
    
    $email_exist = mysql_num_rows($checkemail);
    
    if($email_exist > 0){
        unset($email);
        include 'badlogin.php';
        exit();
    }
    
    
    // lf no errors present with the email
    // data inserted into the database.
    
    
    
    $query = "INSERT INTO ticket_registration (firstname, surname, email, date)
    VALUES('$firstname', '$surname', '$email', '$date')";
    mysql_query($query) or die(mysql_error());
    mysql_close();
    
    // mail user their information
    
    $yoursite = 'www.mydomain.co.uk';
    $webmaster = 'me';
    $youremail = 'info@mydomain.co.uk';
    
    $subject = "You have successfully registered for tickets";
    $message = "Dear $firstname, your ticket details are below, please present this on arrival.  
        Your ticket details as follows:
        
        Name: $firstname
        Email: $email
        Registered on: $date
        Ticket ID: ???????
        Pleaseprint this and bring it along with you on the day as proof of receipt.
    
    
        Thanks,
        $webmaster";
    
    mail($email, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion());
    
    include 'thankyou.php';
    ?>
    
    PHP:
    Were looking at the red bit to display the auto integer from the db:

    Name: $firstname
    Email: $email
    Registered on: $date
    Ticket ID: ???????
    Pleaseprint this and bring it along with you on the day as proof of receipt.
     
    Kayz, Dec 11, 2008 IP
  2. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use mysql_insert_id() function to get the id of the last inserted query
     
    atlantaazfinest, Dec 11, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Use it here:

    
    
    $query = "INSERT INTO ticket_registration (firstname, surname, email, date)
    VALUES('$firstname', '$surname', '$email', '$date')";
    mysql_query($query) or die(mysql_error());
    
    $last_id = mysql_insert_id();
    
    mysql_close();
    
    
    
    PHP:
     
    jestep, Dec 11, 2008 IP
    Kayz likes this.
  4. Kayz

    Kayz Active Member

    Messages:
    245
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Cheers guys for your responses.

    jestep your a life saver mate! One last query thats very minor but not a big issue.. ive got


    Name: $firstname
    Email: $email
    Registered on: $date
    Your Unique Ticket ID: 786$ticketid
    Pleaseprint this and bring it along with you on the day as proof of receipt.



    This works super however the ticket id shows up as "786 xx" notice the space after 786 and then the ticket id? Ive closed the gap as you can see above.. but when the person receives the email it is still showing a gap... anyway we can eliminate that, do you think?

    Cheers
     
    Kayz, Dec 11, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Can you post the code that creates the 786 xx? There's no reason there should still be a space.
     
    jestep, Dec 11, 2008 IP