Find ID Number

Discussion in 'PHP' started by adamjblakey, Apr 9, 2008.

  1. #1
    Hi,

    I have the following which is used whenever a users enters information into my database.

    
    	$title= Clean($_POST[title']);
    		
        mysql_query("INSERT INTO `table` (title) VALUES ('" . $title. "')") or trigger_error(mysql_error(),E_USER_ERROR); 
    
    //Find ID number of above so i can use it in the return URL
    	
    	header("Location: process.php?id=ID HERE");
    	exit();
    PHP:
    What i want to do as indicated above is find the id number of the entry. How would i do that?

    Cheers,
    Adam
     
    adamjblakey, Apr 9, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Assuming that id is a primary key, try:

    
    $title= Clean($_POST['title']);
    $id = '';
           
    mysql_query("INSERT INTO `table` (title) VALUES ('" . $title. "')") or trigger_error(mysql_error(),E_USER_ERROR);
    
    //Find ID number of above so i can use it in the return URL
    $id = mysql_insert_id();
    
    header("Location: process.php?id=$id");
    exit();
    
    PHP:
     
    jestep, Apr 9, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Hi,

    Cheers for that. I have just tried this and although it works, for some reason it destroys a the users session id.

    Any ideas why this would happen?

    Cheers,
    Adam
     
    adamjblakey, Apr 10, 2008 IP
  4. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When you say 'destroys' what do you mean exactly? Does the session id become the insert id or does it become null? The most likely explanation is probably that you've used $id somewhere else and it's overriding the value.

    Jon
     
    Cobnut, Apr 10, 2008 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Works fine now i just changed $id to $last and works a treat.
     
    adamjblakey, Apr 10, 2008 IP