0

How do I insert from 1 form into 2 tables?

I want to submit into tables: gallery and cat.

This is the code I have for just submitting into gallery. How do I add info to the cat table simultaneously?

$sql = 'INSERT INTO gallery (image_url, image_name, image_description)
VALUES (?, ?, ?)';


$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {

$stmt->bind_param('sss', $_FILES['upload']['name'], $_POST['image_name'],     
$_POST['image_description']);
$OK = $stmt->execute();

}

What do I do with this:

$sql_2 = 'INSERT INTO cat (cat_name) VALUES (?)';

3 Answers3

1

After the code you have above, just initialize a new statement, bind it, and execute. You can use the existing connection ($conn). The $sql and $stmt variables have already done their job by inserting into the first table, so just reset them for the second insert.

... your code as posted above, followed by:

$sql = 'INSERT INTO cat (cat_name) VALUES (?)';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
  $stmt->bind_param('s', put your cat_name value here);
  $OK = $stmt->execute();
}

The important thing to remember is that the second insert is just like the first: you have to initialize it, bind it, and execute it.

Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
0

You should use transactions as demonstrated here.

Community
  • 1
  • 1
tixastronauta
  • 404
  • 4
  • 17
-1

No, you can't insert into multiple tables in one MySQL command. You can however use transactions.

BEGIN INSERT INTO users (username, password) VALUES('test', 'test') INSERT INTO profiles (userid, bio, homepage) VALUES(LAST_INSERT_ID(),'Hello world!', 'http://www.stackoverflow.com'); COMMIT;

Have a look at LAST_INSERT_ID to reuse autoincrement values. Edit: you said "After all this time trying to figure it out, it still doesn't work. Can't I simply put the just generated ID in a $var and put that $var in all the MySQL commands?"

Let me elaborate: there are 3 possible ways here:

1/ Is the code you see above. This does it all in MySQL, and the LAST_INSERT_ID in the second statement will automatically be the value of the autoincrement-column that was inserted in the first statement.

Unfortunately, when the second statement itself inserts rows in a table with an auto-increment column, the LAST_INSERT_ID will be updated to that of table 2, and not table 1. If you still need that of table 1 afterwards, we will have to store it in a variable. This leads us to ways 2 and 3:

2/ Will stock the LAST_INSERT_ID in a MySQL variable:

A/ INSERT B/ SELECT LAST_INSERT_ID into @mysql_variable_here C/ INSERT INTO table2 (@mysql_variable_here D/ INSERT INTO table3 (@mysql_variable_here ...

3/ Will stock the LAST_INSERT_ID in a php variable (or any language that can connect to a database, of your choice):

A/ INSERT B/ Use your language to retrieve the LAST_INSERT_ID, either by executing that literal statement in MySQL, or using for example php's mysql_insert_id() which does that for you C/ INSERT ()

Naveen
  • 1,251
  • 9
  • 20
  • 38