Skip to content
Advertisement

insert data in a single row of a database, php and mysql

I need to insert some data in a single row of the database, the way I am doing it creates 2 rows and I only want this data to be entered in a single row.

I can’t find a way to insert the data without creating 2 rows, My question is how can I insert this data into a single row of a database.

context: I have 2 tables in my database, in the first one the specifications of the products such as price, cost and id are inserted, now in the second I want to know 3 data: cost, price and profit. I already have the price and now I want to know the price, that’s what it’s for:

INSERT INTO receipts (cost) SELECT cost FROM products WHERE code=44730

which is to know the cost of a certain item, in this case with the id 44730.

and then I add the other data that with that I have no problems. I already know that I have the insert 2 times and that is my situation I want to know how to introduce that data in a single instruction so that 2 rows are not created.

Here is all the code. I hope this time it is understood, thank you.

  $db = mysqli_connect("localhost", "root", "", "my_database");

  $test = "INSERT INTO receipts (cost) SELECT cost FROM products WHERE code=44730";
  mysqli_query($db, $prueba);

  $sql = "INSERT INTO receipts (price, cost) VALUES ('$price', '$cost')";
  mysqli_query($db, $sql);

Advertisement

Answer

E.g:

"
INSERT INTO receipts (cost,price) 
SELECT cost, :price [or ?]
  FROM products 
 WHERE code=44730
";

prepare, bind $price, and execute the above

Note that there are lots of books and tutorials for this basic stuff.

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