-3

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);

}

  • Post your actual query code so we know what you're fully working with. I'm pretty sure you should be doing something like `$diary_id = $db->lastInsertId();` – Andy Holmes Aug 20 '14 at 09:17
  • PHP is not the most nicely designed language out there, but it won't try to execute random code in strings. And I couldn't find any `last_insert_id()` function in the manual. Seriously. – Álvaro González Aug 20 '14 at 09:17

2 Answers2

0

i am assuming $connect is a database connection link so use mysql_insert_id($connect) then try :

<?php 

$diary_id = mysql_insert_id($connect);

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);
}

?>
-1

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

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38