phpmysqlinsert

How to insert same data into two tables in mysql


Is this possible if I want to insert some data into two tables simultaneously? But at table2 I'm just insert selected item, not like table1 which insert all data. This the separate query:

$sql = "INSERT INTO table1(model, serial, date, time, qty) VALUES ('star', '0001', '2010-08-23', '13:49:02', '10')";   

$sql2 = "INSERT INTO table2(model, date, qty) VALUES ('star', '2010-008-23', '10')";   

Can I insert COUNT(model) at table2? I have found some script, could I use this?

$sql = "INSERT INTO table1(model, serial, date, time, qty) VALUES ('star', '0001', '2010-08-23', '13:49:02', '10')";
$result = mysql_query($sql,$conn);
if(isset($model))
{
  $model = mysql_insert_id($conn);
  $sql2 = "INSERT INTO table2(model, date, qty) VALUES ('star', '2010-008-23', '10')";   
  $result = mysql_query($sql,$conn);
}
mysql_free_result($result);

Solution

  • The simple answer is no - there is no way to insert data into two tables in one command. Pretty sure your second chuck of script is not what you are looking for.

    Generally problems like this are solved by ONE of these methods depending on your exact need:

    Hope this helps