duanjian4150 2014-08-20 09:14
浏览 69
已采纳

从'last_insert_id()'获取值,php函数

I have a problem with getting a value from 'last_insert_id()'.. I want to insert a same id number to id of table 'memo'. However, the problem is after foreach, last_insert_id() gets a new value, so, I tried to make a variable and get a value from 'last_insert_id()', but it didn't work..

$diary_id = 'last_insert_id()';

foreach ($_POST['memo'] as $memo) {
     echo "You selected: $diary_id <br>";
     echo "You selected: $memo <br>";

 $query_test = "insert into memo(memo_no,id,memo)
    values(NULL,$diary_id,'$memo')";
 mysql_query($query_test, $connect);

}

  • 写回答

2条回答 默认 最新

  • duanruanxian5028 2014-08-20 09:20
    关注

    Use mysql_insert_id function in php to get the last previous AUTO INCREMENTED generated id. Use the code below

    $diary_id = mysql_insert_id();
    
    foreach ($_POST['memo'] as $memo) {
         echo "You selected: $diary_id <br>";
         echo "You selected: $memo <br>";
    
     $query_test = "insert into memo(memo_no,id,memo)
        values(NULL,$diary_id,'$memo')";
     mysql_query($query_test, $connect);
    
    }
    

    Hope this helps you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?