php - 三个插入语句插入三个表

标签 php mysql

我有以下插入语句:

$sql ="INSERT INTO `firm`(name, VAT, active) VALUES ('$name', '$VAT', '$active')"; 

$sql = "INSERT INTO `area`(name, hub_name, fk_hub_id) VALUES ('$areaname',(SELECT `name` from hub WHERE name = '$hub_name'), (SELECT `id` from hub WHERE name = '$hub_name'))";

$sql ="INSERT INTO 'contactdetails'
        (fk_firm_id,
         address_physical_line_1,
         address_physical_line_2,
         address_physical_line_3,
         address_physical_line_4,
         address_physical_line_5,
         address_physical_line_6,
         address_physical_line_7,
         address_physical_code,
         address_postal_line_1,
         address_postal_line_2,
         address_postal_line_3,
         address_postal_line_4,
         address_postal_line_5,
         address_postal_line_6,
         address_postal_line_7,
         address_postal_code,
         fax_1,
         fax_2,
         phone_1,
         phone_2,
         phone_3,
         phone_4)
VALUES      ( (SELECT `id`
           FROM   firm
           WHERE  name = '$name'),
          '$address_physical_line_1',
          '$address_physical_line_2',
          '$address_physical_line_3',
          '$address_physical_line_4',
          '$address_physical_line_5',
          '$address_physical_line_6',
          '$address_physical_line_7',
          '$address_physical_code',
          '$address_postal_line_1',
          '$address_postal_line_2',
          '$address_postal_line_3',
          '$address_postal_line_4',
          '$address_postal_line_5',
          '$address_postal_line_6',
          '$address_postal_line_7',
          '$address_postal_code',
          '$fax_1',
          '$fax_2',
          '$phone_1',
          '$phone_2',
          '$phone_3',
          '$phone_4')  ";

我是否必须使用事务语句来运行这三个查询。我从未使用过交易报表。一个陈述取决于其他陈述的值。

最佳答案

MySQL 默认将 AUTO_COMMIT 设置为 true。这意味着脚本中的每个查询都将在紧随其后的查询之前执行。

这允许您执行以下操作:

// Here I admit that the table is empty, with an auto-incremented id.
INSERT INTO test VALUES ('', 'First');
INSERT INTO test ('', SELECT value FROM test WHERE id = "1");

在这里,您将插入 id=1, value="First" 的第一行,然后插入 id=2, value="First"

关于php - 三个插入语句插入三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18080702/

相关文章:

php - 如果代码中有任何错误,如何将值插入到数据库表中?

mysql - Sql实现方法选择

mysql - 在另一个函数中使用 sql 连接

php - 在数据库中保存评级

php - 将可变价格范围替换为 WooCommerce 3 中选择的变化价格

PHP 代理服务器

php - 熟悉 XAMPP

php - WordPress 3.8 : Image upload is not possible

MySql 闭包表不支持不同父级的重复子类别

python - 将 django 的 auth_user 与现有用户表合并