Skip to content
Advertisement

MYSQL Get id from Table1 and Insert to Table2 while INSERTING Values (USER Form)

I have 2 tables:

Table1

and Table2

had to insert values to table2 with u_id when user enters from form Example:I need to add the value table1 u_id while that user is logged in into table2 that is id increments in table and u_id from table1 and values as user enters from table2

i tried something this query however not successful

Advertisement

Answer

The problem with your query is that the subquery returns multiple rows (one value per row in table1), while the database expects a scalar value.

I would expect that you know the email of the current user, and want to recover the corresponding u_id to insert a new row in table2, along with a literal value. If so, I would recommend the insert ... select syntax:

Side note: values is a SQL keyword, hence not a good choice for a column name (you would need to quote this identifier every time you use it). I renamed this column to val in the query.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement