Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    6

    Unanswered: Inserting value in table

    I have the following query:

    Code:
    mysql_query("INSERT INTO messages (id, exped, uid, subiect, message, send_date) VALUES (0, '".$_SESSION['username']."', '".$fetch['user_id']."', '".$_POST['subiect']."', '".$_POST['mesaj']."', NOW())");
    The query insert some data to mysql table called "messages".
    I have an ID collumn in "messages table" with following option: id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

    VALUES (0,.... 0 (the ID) auto increment at every row added to table.

    When a new row added to table "messages", I need to get the ID of row and add to another table called "private"

    I use this method:

    Code:
    mysql_query("INSERT INTO messages (id, exped, uid, subiect, message, send_date) VALUES (0, '".$_SESSION['username']."', '".$fetch['user_id']."', '".$_POST['subiect']."', '".$_POST['mesaj']."', NOW())"); 
    
    $msg = mysql_query("SELECT id FROM messages WHERE message='".$_POST['mesaj']."'"); 
    $msg_result = mysql_fetch_array($msg);
    But is not good idea when $_POST['mesaj'] already exist in table.

    Anyone help me a bit ? Any idea?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    see the LAST_INSERT_ID function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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