PHP another mysql_insert_id() not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JnrJnr
    New Member
    • Oct 2009
    • 88

    PHP another mysql_insert_id() not working

    Hi,
    I have one database connection
    I am using an insert statement
    The insert statement inserts successfully
    Right after the insert I want the ID of the newest row
    My database table has a unique ID and set to auto inc
    mysql workbench version is 5.2.34
    Code:
    $save = mysqli_query($connection,"insert into table (message, title, newdate) values ('$Message', '$itle', CURDATE())");
    	$newid =  mysql_insert_id($connection);echo('val is '. $newid);
    The value of $newid is always empty or 0
    Please help!
  • Joseph Cobham
    New Member
    • Jan 2012
    • 23

    #2
    why don you try some thing of this nature it works perfectly for me

    Code:
    $query = "INSERT into `table` (`message`,`title`,`newdate`)
    	VALUES
    ('$message','$title', CURDATE())";
    
    mysql_query($query,$connection) or die("Insertion Failed:" . mysql_error());
    $newest_id = mysql_insert_id($connection);
    and dont forget to escape your query data using mysql_real_esca pe_string()

    If you find any error let me know

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      in case you didn’t notice:
      Code:
       $save = mysqli_query($connection,"insert into table (message, title, newdate) values ('$Message', '$itle', CURDATE())");
      // -----------^ 
      $newid = mysql_insert_id($connection);echo('val is '. $newid);
      // -----------^
      Last edited by Dormilich; Feb 12 '12, 12:34 PM.

      Comment

      Working...