Navigation

    APPDRAG Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular

    REMINDER

    Please be respectful of all AppDragers! Keep it really civil so that we can make the AppDrag community of builders as embracing, positive and inspiring as possible.

    SOLVED why is the function LAST_INSERT_ID() not working for me

    Cloud Backend (Cloud DB, API Builder)
    2
    3
    350
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      malky shlomowith last edited by

      I want to use the ID that was generated for one table and insert it into a second table and i am getting an error.

      error: "SQL Syntax Error near 'INSERT INTO all_tokens (user_id, device, token)\n" +
      " VALUES (LAST_' at line 3",

          let sql = `INSERT INTO users (name, nickname, email, password, 
                                               last_logged, current_token ) 
                         VALUES ('${name}', '${nickname}', '${email}', 
                                      '${hachPassword}',now(), '${token}' )  
                        INSERT INTO all_tokens (user_id, device, token)
                       VALUES (LAST_INSERT_ID(), '1' ,'${token}')`
                     
          await cloudbackend.sqlExecuteRawQuery(sql).then(async res => {
      
              let result = await JSON.parse(res);
              
               console.log(result)
      
              if (result.affectedRows == 2 ) {
               // do something
      

      }

      David Alimi 1 Reply Last reply Reply Quote 0
      • David Alimi
        David Alimi @malky shlomowith last edited by

        Hello @malky-shlomowith,

        First of all, we can't use 'LAST_INSERT_ID (),' as a variable directly. We need to use SELECT to fetch this ID.

        Also, I advise you to make two separate requests following this diagram:

        Screen Shot 2021-08-23 at 10.41.14.png.

        • This is the result of our first query where we get the userID :

        Screen Shot 2021-08-23 at 10.42.34.png

        • And then you can call the second query using the userID.

        Do not hesitate if you have any questions 🙂
        Have a nice day 🌻

        1 Reply Last reply Reply Quote 1
        • M
          malky shlomowith last edited by

          Thank you !!!

          This was a big help!!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post