6

I have 2 tables that I am trying to insert data into using PHP

Table: WINE
-----------------------------------------------------------------
|  wine_id  |  wine_type  | country  |  indicator  |  color  |
-----------------------------------------------------------------

wine_id is auto incremented, then This is my other table

Table: stock
 --------------------------------------
 |  stock_id  |  wine_id  | quantity  |
 --------------------------------------

For the STOCK table I have stock ID as Auto incremented and wine_id is foreign-key so All i need to insert is quantity.

I have a syntax like this:

$sql = mysql_query("INSERT INTO TABLE1(field1, field2, field3) VALUES('value1',value2,value3) INSERT INTO STOCK(field) VALUES ('value1')");

If there is another way to do so please suggest and I would like some examples please.

1

4 Answers 4

7

You need to separate your two INSERT statements with a semicolon.

This(mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !

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

3 Comments

That's what I was going to say
i do not think that mysql_* support multiple query. stackoverflow.com/questions/11106719/…
I can't change my code to mysql* as I have done plenty work into it what do you suggest me doing? @shankar Damodaran
2

You can run multiple INSERTS once, however you cannot insert into two tables from one single INSERT Statement. Also make sure to get the last inserted ID with LAST_INSERT_ID() for the foreign key and use a semicolon ; between the two INSERTS

Comments

2

From the looks of it, what you're trying to do is:

  1. Insert data into the wine table
  2. Get the ID from the wine table
  3. Insert the ID into the stock table

mysql_query does not support multiple queries. So, my suggestion would be:

$result = mysql_query("INSERT INTO `wine` (`wine_type`, `country`, `indicator`, `colour`) VALUES ('Merlot', 'Australia', 'Dry', 'Red')");
$result = mysql_query("INSERT INTO `stock` (`wine_id`, `quantity`) VALUES ('".mysql_insert_id()."', '0');");

Modifying of course to take into account your own variables and value sanitation. As has been mentioned, the mysql_ functions are being deprecated, and you're best to move to a PDO structure for your database in the near future.

3 Comments

where would we get mysql_insert_id()
@BlueLeaf It's a function in PHP. However, as I commented three years ago, the mysql_ functions are deprecated, and you are best to look at PDO alternatives.
how can we use bind_param in this case?
1

you can do it by multiple statement details

like this:

$sql = "INSERT INTO TABLE1(field1, field2, field3) VALUES('value1',value2,value3); INSERT INTO STOCK(field) VALUES ('value1');";

multi_query($conn,$sql);

you need to use mysqli_* for this. I assume $conn is your mysqli connection

3 Comments

Wher did you get $conn from? @Awlad Liton
@user3311898, This answer won't work for you as you are using mysql_* functions.
@user3311898: you must have to use mysqli_* for this. see details in the given link and my updated answer

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.