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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts

    Find an id before it's created

    This is a very puzzling question. When someone creates a new alliance, the ID is an auto increment. However I need to also add a bank, but need to know how to pull the id that was just created to insert into the next table. Any ideas? Because my idea isn't a good one. Thanks

    PHP Code:
    <?php
    mysqli_query
    ($link"INSERT INTO rod_alliances SET id='', name='$name', membercount='1', password='', private='0', description='$description', founder='".$char['charname']."', icon='http://www.newrodgame.com/images/defaultalliancelogo.jpg'");
                        
     
    //   mysqli_query($link, "INSERT INTO rod_alliancebank SET allianceid='', name='$name', gold='1000'");
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    mysql_insert_id()
    I can't really think of anything to write here now...

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    That's exactly what I need! So I am guessing I'd do something similar to this?

    PHP Code:
    mysqli_query($link"INSERT INTO rod_alliancebank SET allianceid='mysql_insert_id($link,$query) ', name='$name', gold='1000'"); 
    However how would I add a variable to it? Because if I do

    PHP Code:
    $query mysqli_query($link"INSERT INTO rod_alliances SET id='', name='$name', membercount='1', password='', private='0', description='$description', founder='".$char['charname']."', icon='http://www.newrodgame.com/images/defaultalliancelogo.jpg'"); 
    The query won't run because it's turned into a variable?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    New to the CF scene
    Join Date
    Mar 2014
    Location
    Bangalore
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please explain briefly what is your concept exactly.
    your concept is insert a parent id in to another table?

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    So I am guessing I'd do something similar to this?
    you cannot execute a PHP function inside a string. besides that, you obviously have to wait for the ID to be inserted before you can fetch it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    Good point, so mysqli_insert_id would come after the query. So perhaps this?

    PHP Code:
     <?php
    mysqli_query
    ($link"INSERT INTO rod_alliances SET id='', name='$name', membercount='1', password='', private='0', description='$description', founder='".$char['charname']."', icon='http://www.newrodgame.com/images/defaultalliancelogo.jpg'");

    $newid mysqli_insert_id();
                        
      
    mysqli_query($link"INSERT INTO rod_alliancebank SET allianceid='$newid', name='$name', gold='1000'");
    But where do I add $link for the DB Connection?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    But where do I add $link for the DB Connection?
    before that?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    PHP Code:
    $newid mysqli_insert_id();
                        
    mysqli_query($link"INSERT INTO rod_alliancebank SET allianceid='$newid', name='$name', gold='1000'"); 
    An autoincrement field is *ALWAYS* an integer of some sort. So why in the world would you make the matching field in rod_alliancebank.allianceid be a string of some sort???

    If you foolishly did that, go change allianceid to an int or bigint.

    I suspect that the field gold is also a number field. So why assign a string to it?

    In other words, I want to see
    Code:
    mysqli_query($link, "INSERT INTO rod_alliancebank SET allianceid=$newid, name='$name', gold=1000");
    ]

    Yes, MySQL (and PHP) is SLOPPY enough to allow you to use strings in place of numbers (and so MySQL or PHP will convert them to numbers for you), but IT IS A BAD PRACTICE to use the wrong data type! Try to use the appropriate values for the given fields.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    Thanks for clearing that up. Yes allianceid and gold are numbers not strings. I guess I've always just been lazy about it. I'll make the changes.
    See this is helpful to me, I am learning something.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #10
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    so a new alliance is being made and the auto increment id is working fine. However nothing is being made under alliancebank. Nothing is inserted into the database and there is no errors in the error log.

    PHP Code:
    mysqli_query($link"INSERT INTO rod_alliances SET id='', name='$name', membercount='1', password='', private='0', description='$description', founder='".$char['charname']."', icon='http://www.newrodgame.com/images/defaultalliancelogo.jpg'");
                         
    //   mysqli_query($link, "INSERT INTO rod_alliancebank SET allianceid='', name='$name', gold='1000'");
                            
    $newid mysqli_insert_id($link);
                            
    mysqli_query($link"INSERT INTO rod_alliancebank SET allianceid=$newid, name='$name', gold=1000"); 
    This is the PRINT of the alliancebank query.

    PHP Code:
    INSERT INTO rod_alliancebank SET allianceid=5name='LastTest'gold=1000 
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    1) which error log?
    2) you can fetch the last mysql error explicitly.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #12
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Can you take your 2 queries and execute them manually?

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by myfayt View Post
    This is the PRINT of the alliancebank query.

    PHP Code:
    INSERT INTO rod_alliancebank SET allianceid=5name='LastTest'gold=1000 
    I may be wrong however I didn't think you could use set in an insert query.

    The way I've always done it is:

    insert into [table] (`columns`) values ('values')
    I can't really think of anything to write here now...

  • #14
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    Quote Originally Posted by Daniel Israel View Post
    I may be wrong however I didn't think you could use set in an insert query.

    The way I've always done it is:

    insert into [table] (`columns`) values ('values')
    SET works fine on other queries, it's just a different style.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #15
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Quote Originally Posted by tangoforce View Post
    I may be wrong however I didn't think you could use set in an insert query.

    The way I've always done it is:

    insert into [table] (`columns`) values ('values')
    It's an alternate way of INSERTing. It's one of those MySQL quirks that gives me fits. LOL.

    Again, though, should be dropped into PHPMySQLAdmin, it usually gives pretty good feedback on your query.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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