0

Is this possible? I need to run the result of one sql statement into another..

$result= mysqli_query($dbconn,"Select max(cartid) from cart;");
$row = mysqli_fetch_assoc($result);

$sql1 = "insert into cart(cartid) values('".$row['max(cartid)']."');";
mysqli_query($dbconn,$mysql1);

EDIT:

Added in my sql tables to show a better example of what im trying to do This is my cart tableenter image description here

This is my transaction table enter image description here

Basically I want to transfer the row where cartid = 5 from cart table, into the transaction table where the new transaction id will be = 5. As you can see, the transaction table also has a cartid. Not sure how im going to bring over this cartid and insert into the transaction table as a new row. FYI, in order to get cartid=5 i would like to use order by cartid desc limit 1 to get the last possible row in the cart table

The new transaction table's row will look something like this.

transactionid outletid nric cartid transactiondate totalbeforediscount totalafterdiscount transactiontime username
     5           0      0     5         0                 0                       0                 0            0
2
  • Is the only purpose for retrieving the data in the first query ($result) to then use it in the second - or - will you be using that data elsewhere as well? Commented Jul 24, 2016 at 5:27
  • @Nicarus I would also be using it elsewhere as well Commented Jul 24, 2016 at 5:43

1 Answer 1

2

You can combine the queries into one.

Please give it a try:

INSERT INTO cart(cartid) SELECT MAX(cartid) FROM cart

WORKING DEMO

EDIT:

In order to insert mulitiple values like that your query would look like below:

INSERT INTO cart(cartid,rowid) SELECT MAX(cartid),MAX(rowid) FROM cart

WORKING DEMO

But I don't understand why are trying to insert the columns having max value in the same table. Since your table contains those already.

Edit2:

INSERT INTO transaction_table(cartid) SELECT MAX(cartid) FROM cart;

Sign up to request clarification or add additional context in comments.

9 Comments

hmm, how about if lets say i want to insert multiple values from different tables? Maybe like this? "Insert into cart(cartid,rowid) Select Max(cartid) from cart,Select Max(rowid) from row"
What if my rowid comes from another table? Sorry im just using an example
Then I guess you need to share actually what are you intending to do.
I will be getting a few result from some sql statements. And i want to place all these results together into one insert sql statement. So i will then be inserting values of these different results i got, into one sql table. However the results that i am getting are mostly from other tables.. Sorry if i'm not explaining it well enough. I'm not sure why the way i'm doing isnt correct, by storing each result into a PHP variable then using it in another SQL statement
Then please share the table structures of all the related tables. Without prior knowledge of which data come from which table I cannot provide any help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.