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
-
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
}
-
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:
.
- This is the result of our first query where we get the userID :
- And then you can call the second query using the userID.
Do not hesitate if you have any questions
Have a nice day -
Thank you !!!
This was a big help!!