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

    Can't retrieve ID of row just posted

    I'm having a similiar problem to this thread (Please help with PHP based contact form.) but not exactly as i can't retrieve the AI generated ID of the row i just posted to.

    I have two pages (HTML):

    Code:
      <form method="post" class="uniForm" action="global/guest.php">
    
    <fieldset class="inlineLabels">
      
      <div class="ctrlHolder">
        <label for="FullName"><em>*</em>Full Name</label>
        <input name="FullName" id="FullName" value="" size="35" maxlength="30" type="text" class="textInput" />
        <p class="formHint">E.g.: Jonathan Doe</p>
      </div>
      
      <div class="buttonHolder">
              <button type="submit" name="submit" value="submit" class="primaryAction">Submit</button>
            </div>
     
    	
    </fieldset>
    
    </form>
    
    ...and thats repeated a couple times but etc. etc. w/e
    Page two, where the results from the form are being posted and inserted into the db and than displayed

    PHP Code:
    <?php

    $FullName 
    $_POST["FullName"];


    mysql_connect ("localhost""user""pass") or die ('Could not connect');
    mysql_select_db ("dbname");


    $query="INSERT INTO guest 

    (ID, FullName)

    VALUES

    ('NULL', '"
    .$FullName."')";

    mysql_query($query) or die ('data was not submitted to db');



    echo 
    "this is the ID: "$ID.

    <table>
    <tr><td><b>Full Name:</b></td><td> "
    $FullName." </td></tr>
    </table>"
    ;


    ?>
    but the id does not appear where i have the echo to show the id


    can this not be done on the same page as the post and insert?

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts

  • Users who have thanked met for this post:

    thilss0o (07-03-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You don't automatically get an ID back after making an insert query. Specially not into a predefined variable. Think about it, if someone was using the $ID variable before making a query, they'd then lose the value of whatever's in it. Just use the php function mysql_insert_id or more generically, the mysql function LAST_INSERT_ID to retrieve the last inserted id.

  • Users who have thanked Keleth for this post:

    thilss0o (07-03-2010)

  • #4
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thanks guys, i figured it would be something of that nature, but wasn't familiar with it


  •  

    Posting Permissions

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