SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: error on insert

  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error on insert

    Hi, I am trying to insert values from an array into a table with the last id from the last insert... however I get an error:

    "Duplicate entry '1' for key"

    PHP Code:
    $sql="INSERT INTO properties (property_id, name) VALUES('','$name')";
    $result=mysql_query($sql) or die(mysql_error(). "property");

    $propertyID mysql_insert_id();
    foreach (
    $paymentOption as $value) { 
         
    $sql_payment="INSERT INTO payment_options VALUES('$propertyID','$value')";
         
    $result_payment=mysql_query($sql_payment) or die(mysql_error(). "payment");


  2. #2
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you have to tell mysql_insert_id() which connection you meant.

    mysql_insert_id($result);

    Might be the answer.

  3. #3
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure, but it seems like the field property_id isn't set to auto_increment.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  4. #4
    SitePoint Evangelist nsj's Avatar
    Join Date
    Oct 2005
    Location
    Jamaica (W.I)
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql="INSERT INTO properties (property_id, name) VALUES('','$name')";
    $result=mysql_query($sql) or die(mysql_error(). "property");

    Dont do this.

    Set property id to be auto increment and issue the following command:
    $sql="INSERT INTO properties (name) VALUES('$name')";
    $result=mysql_query($sql) or die(mysql_error(). "property");
    # whatever else here.

    That should work

  5. #5
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that was it... I added that to the code and get this error...

    mysql_insert_id(): supplied argument is not a valid MySQL-Link resource

    PHP Code:
    $sql="INSERT INTO properties (property_id, name) VALUES('','$name')";
    $result=mysql_query($sql) or die(mysql_error(). "property");

    $propertyID mysql_insert_id($result);
    foreach (
    $paymentOption as $value) { 
         
    $sql_payment="INSERT INTO join_properties_payment_options VALUES('$propertyID','$value')";
         
    $result_payment=mysql_query($sql_payment) or die(mysql_error(). "payment");


  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nsj, both methods will work, but the second one is better to use IMO.
    ERIK RIKLUND :: Yes, I've been gone quite a while.


Bookmarks

Posting Permissions

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