0

I am trying to create a variable with one id of the last insert that I do and I am trying to past it to another page.

In the first page this is code:

$insert= "INSERT INTO resources (name,description,place,time) VALUES ('$name','$description','$place',CURDATE())";
        mysql_query($insert);
        $last_id= mysql_insert_id();
        header("Location:new_page.php?last_id=$last_id");

And in the second page this is my code:

<?php
    echo "This is id of resource : $last_id";
?>

I can't do to work it, any solution?

Thanks!

Regards

WhiteShadow
  • 103
  • 2
  • 10
  • $_SESSION variables will help you out of it. http://php.net/manual/en/reserved.variables.session.php – Falt4rm Jun 04 '15 at 15:43
  • To access last_id from the URL new_page.php?last_id=$last_id you have to use the variable $_GET['last_id'] – adrien54 Jun 04 '15 at 15:56

3 Answers3

1

You have to retrieve the information from the $_GET data, it won't be a variable automatically.

<?php
    echo "This is id of resource : " . $_GET['last_id'];
?>
Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
  • Thanks so much for your answer. I have tryed it and I have received this error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) How can I solve it? – WhiteShadow Jun 04 '15 at 16:03
  • Finally I can solve it with echo "This is id of resource" . $_GET['last_id'] Thanks for your help!! Regards – WhiteShadow Jun 04 '15 at 16:05
0

You can't pass variable through more pages like this.

You need to write it using $_GET, because it's in URL.

echo $_GET['last_id'];

If you want to work with it later, use SESSION or COOKIE.

pavel
  • 26,538
  • 10
  • 45
  • 61
  • Thanks so much for your answer. I have tryed it and I have received this error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) How can I solve it? – WhiteShadow Jun 04 '15 at 16:03
  • @WhiteShadow: and your code which returns this error is? – pavel Jun 04 '15 at 16:04
  • Finally I can solve it with echo "This is id of resource" . $_GET['last_id'] Thanks for your help!! Regards – WhiteShadow Jun 04 '15 at 16:05
  • @WhiteShadow: before you forget probably `.` between two strings or any similar typo. If this answer helped you, please answer it as accepted for future readers. – pavel Jun 04 '15 at 16:08
  • No, I don't forget it, I have all in a only string and this was the problem. I have accept this answer, thanks for the help! – WhiteShadow Jun 04 '15 at 16:18
0

you can also use $_REQUEST['last_id']

<?php
    echo "This is id of resource :".$_REQUEST['last_id'];
?>

In this way you can access both get and post method variable

Yes Kay Selva
  • 584
  • 4
  • 14
  • Using $_REQUEST when you don't absolutely have to isn't a good habit to get into. [When and why should $_REQUEST be used instead of $_GET / $_POST / $_COOKIE?](http://stackoverflow.com/questions/107683/when-and-why-should-request-be-used-instead-of-get-post-cookie) – Geoff Atkins Jun 04 '15 at 15:51
  • yeah i know that but i just say this is one of method. – Yes Kay Selva Jun 04 '15 at 15:54
  • Hi, thanks for your answer, finally I can solve it using $_GET – WhiteShadow Jun 04 '15 at 16:06