0

I think it should be possible, but i dont have a clue how to write it.

  • 1st statement: Select multiple data from table1 and copy them to table2.
  • 2nd statement: Save a variable to table2.

both statements should be saved in the same table row.

here is my code:

foreach($_POST["checkbox"] as $id){

            $query ="INSERT INTO table2 (name, number, price, quantity, sumPrice, supplier, customer)
                    SELECT name, number, price, quantity, sumPrice, supplier, customer
                    FROM table1
                    WHERE table1_id = '".$id."'";

                    "INSERT INTO table2(adoptedBy)
                    VALUES ('$name')";

            $result = mysqli_query($db_link, $query);   

what did i wrong?

Yosshi
  • 3
  • 2

2 Answers2

0

Meh.. If you need explanation, there you go. You mentioned, that you want to insert a single row and then update it. Since the INSERT statement is inserting a new row to the table (self-explained..), you need to use an UPDATE statement, which updates a specific row with new values.

So you'll first insert a new row with INSERT and then update the 'adoptedBy' field with your new value.

Example:

"UPDATE table2 SET adoptedBy='$name' WHERE --Here you refer to the row's primary id";

After the WHERE clause you just identify somehow your row (e.g. primary ID, which is not shown in the OP)

Hienz
  • 688
  • 7
  • 21
0

1st statement: Select multiple data from table1 and copy them to table2.

You will need to use INSERT INTO SELECT which you did well.

$query ="INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
                    SELECT name, `number`, price, quantity, sumPrice, supplier, customer
                    FROM table1
                    WHERE table1_id = '".$id."'";

2nd statement: Save a variable to table2.

One thing you did not notice is that after WHERE table1_id = '".$id."'"; you terminated $query therefore this never runs `

"INSERT INTO table2(adoptedBy)
                    VALUES ('$name')";

what you can do is to use mysqli_multi_query();

Then you query will be something like :

 <?php

    $query = "INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
                    SELECT name, `number`, price, quantity, sumPrice, supplier, customer
                    FROM table1
                    WHERE table1_id = '" . $id . "';";

    $query .= "UPDATE table2 SET adoptedBy =".$name."  where id =  LAST_INSERT_ID()";

    $result = mysqli_multi_query($db_link, $query);


    if ($result) {

        echo "success";
    } else {

        echo "Error : " . mysqli_error($db_link);
    }

?>

Hope this helps.

NB: I advice that you start learning doing sql queries using prepared statements whether you are using mysqli prepared or pdo prepared statements

Edit :

Alternative when the above suggestion does not work,

You may update the LAST_INSERT_ID() when the first query run successfully get that last inserted id then do an update on that id

<?php


    $query = "INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
                    SELECT name, `number`, price, quantity, sumPrice, supplier, customer
                    FROM table1
                    WHERE table1_id = '" . $id . "';";
    $result = mysqli_query($db_link, $query);


    if ($result) {

        $query = "UPDATE table2 SET adoptedBy ='".$name."' WHERE id = LAST_INSERT_ID();";
        if(mysqli_query($db_link,$query)){

            echo "success";
        }else{

            echo "update failed".mysqli_error($db_link);
        }
    } else {

        echo "Error : " . mysqli_error($db_link);
    }

?>
Community
  • 1
  • 1
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34