0

I am new in databases but I know how to insert data into one table with sql and php, but now I need to insert data from an array into few tables at the same time. More, I have 3 main tables and 2 linking tables , there is a linkng table between 1 and 2 and another one between 2 and 3. How to insert data into the datables simultaneously? The primary key in linking tables constitute from primary 'id' keys from main tables which are set on auto increment. Thanks for any help guys..

7
  • After each insert statement you can get the id by calling mysql_insert_id. That should help you out.
    – TJHeuvel
    Aug 15, 2011 at 14:33
  • You can get better help if you post snippets of relevant code.
    – Sukumar
    Aug 15, 2011 at 14:34
  • You can use stored procedures to insert data into mysql tables. This approach is specially helpful in these kinds of scenarios..... Aug 15, 2011 at 14:35
  • @Sayem Ahmed what do you mean by stored procedures in this case?
    – anna
    Aug 15, 2011 at 14:38
  • @ TJHeuvel can you give me an examle of how to connect 2 main tables and a linking table betwwen them plz
    – anna
    Aug 15, 2011 at 14:40

3 Answers 3

0

You can't insert records in more than one table at one time. You have to do it once for table manually.

0

You can't insert data into many tables using one INSERT statement if that's what you mean by simultaneously. Write a series of INSERT statements, one for each table. Do checks to make sure each one has run successfully.

0

You can insert data into table one by one only. But if you need to guarantee that data exist in all tables at once you should use transaction. It will help if something is getting wrong and you don't get inconsistent data in db. For example, in php you make it like this one:

$pdo_con = new PDO(...);
try {
$pdo_con->beginTransaction();

$pdo_con->exec("INSERT INTO table1 ....");
$table1_id = $pdo_con->lastInsertId();
$pdo_con->exec("INSERT INTO table2 ....");
$table2_id = $pdo_con->lastInsertId();
$pdo_con->exec("INSERT INTO linked_table .... values($table1_id, $table2_id )");
//repeat for other tables

$pdo_con->commit();
catch(PDOException $e) {
$pdo_con->rollback();
}
4
  • thanks for this answer!!!can you tell me what happens if lets say adress already exists in an address table? is it going to take previously established address_id and take it to a lnking table or what?
    – anna
    Aug 15, 2011 at 16:48
  • :can I use $pdo_con if I have realtional database not object oriented?
    – anna
    Aug 15, 2011 at 18:05
  • Sure. PDO is only wrapper on different drivers which are used for interaction with db server. See php.net/manual/en/intro.pdo.php
    – Andrej
    Aug 16, 2011 at 7:17
  • Regarding previous comment, if you try to insert a record in table with the same unique key(address in this this case) you will get db error. So for catching this situation try to select data from table by this address and then only start to make inserts in appropriate tables.
    – Andrej
    Aug 16, 2011 at 7:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.