【问题标题】:How do I use the value from the latest insert using mysql_insert_id in subsequent MULTIPLE insert statements (in PHP and MYSQL)?如何在后续的 MULTIPLE 插入语句(在 PHP 和 MYSQL 中)中使用 mysql_insert_id 使用最新插入的值?
【发布时间】:2012-04-30 12:04:01
【问题描述】:

我想将 TABLE1 中最新插入的值添加到包含多个条目的 TABLE2 中的后续 MULTIPLE insert 语句中 - 但是在 MySQL 中,对于添加到 TABLE2 中的每个条目,我只得到一个 0(零)。

我知道在执行第一个 MySQL 查询后,我需要将 mysql_insert_id 存储在一个变量中。所以我在第一个mysql_query() 语句之后包含了一个名为$post_id 的变量,如下所示:

// If the submit button is pressed in the HTML form
if (isset($_POST['submit_event'])) {
    // First SQL Statement
    if (!mysql_query($sql1,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "SQL 1 SUCCESS! 1 record added<br/>";

    // A variable to store the id from the last MySQL query
    // This is the first time I have declared this variable
    $post_id = mysql_insert_id();

    // Second SQL Statement which utilises the variable
    if (!mysql_query($sql2,$con))
     {
      die('Error: ' . mysql_error());
    }
    echo "SQL 2 SUCCESS! 1 record added<br/>";
    echo "Finito!<br/>";
    mysql_close($con);
}

我写的这个SQL多次插入TABLE2语句如下:

$sql2="INSERT INTO TABLE2 (post_id, meta_key, meta_value)
    VALUES
    ($post_id,'key 1','value 1'),
    ($post_id,'key 2','value 2'),
    ($post_id,'key 3','value 3')
";

然而,尽管如此(看起来正确),当我实际查看 MySQL 时,TABLE2 中每个条目的 post_id 显示为 0(零)?

我哪里错了?!帮助!

【问题讨论】:

  • 显然是$post_id 0,所以你想知道什么?见php.net/mysql_insert_id
  • $sql1 没什么特别的,TABLE1 包含一个 AUTO_INCREMENT 字段,如下:$sql1="INSERT INTO TABLE1 (column1, column2, column3) VALUES (1,'data1','data2')";
  • TABLE1的数据库定义是什么?请将其添加到您的问题中。什么给var_dump($post_id)var_dump($sql2) 给了什么?在获得$post_id 之前如何构建$sql2?请在脚本顶部添加error_reporting(~0); ini_set('display_errors', 1);
  • 我终于找到了答案!我意识到,因为我将$sql2 字符串语句放在$post_id = mysql_insert_id(); 之前,所以PHP 无法将其插入代码中。所以正确的做法是:
  • 这行得通...if (isset($_POST['submit_event'])) { // First SQL Statement if (!mysql_query($sql1,$con)) { die('Error: ' . mysql_error()); } echo "SQL 1 SUCCESS! 1 record added&lt;br/&gt;"; $post_id = mysql_insert_id(); $sql2="INSERT INTO TABLE2 (post_id, meta_key, meta_value) VALUES ($post_id,'key 1','value 1'), ($post_id,'key 2','value 2'), ($post_id,'key 3','value 3') "; // Second SQL Statement which utilises the variable if (!mysql_query($sql2,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); }

标签: php mysql sql


【解决方案1】:

这可能是一个愚蠢的问题,但从您的代码中并不清楚:您是否将$post_id 值插入到$sql2 字符串中?

$sql2="INSERT INTO TABLE2 (post_id, meta_key, meta_value)
    VALUES
    (" . $post_id . ",'key 1','value 1'),
    (" . $post_id . ",'key 2','value 2'),
    (" . $post_id . ",'key 3','value 3') "; 

【讨论】:

  • 是的,正确 - 我将 $post_id 插入到 $sql2 字符串中!我应该按照你上面写的方式做,还是我的方式是一个同样有效的例子?
【解决方案2】:
$sql2="INSERT INTO TABLE2 (post_id, meta_key, meta_value)
    VALUES
    ({$post_id},'key 1','value 1'),
    ({$post_id},'key 2','value 2'),
    ({$post_id},'key 3','value 3')
";

【讨论】:

    【解决方案3】:

    我终于找到了答案!我意识到,因为我将$sql2 字符串语句放在$post_id = mysql_insert_id(); 之前,所以PHP 无法将其插入代码中。所以正确的做法是:

    // If the submit button is pressed in the HTML form
    if (isset($_POST['submit_event'])) {
    // First SQL Statement
    if (!mysql_query($sql1,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "SQL 1 SUCCESS! 1 record added<br/>";
    
    // A variable to store the id from the last MySQL query
    // This is the first time I have declared this variable
    $post_id = mysql_insert_id();
    
    // Place the SQL statement AFTER the mysql_insert_id() variable
    $sql2="INSERT INTO TABLE2 (post_id, meta_key, meta_value)
    VALUES
    ($post_id,'key 1','value 1'),
    ($post_id,'key 2','value 2'),
    ($post_id,'key 3','value 3')
    ";
    
    // Second SQL Statement which utilises the variable
    if (!mysql_query($sql2,$con))
     {
      die('Error: ' . mysql_error());
    }
    echo "SQL 2 SUCCESS! 1 record added<br/>";
    echo "Finito!<br/>";
    mysql_close($con);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多