1

I am currently working on school project and am developing a simple discussion forum. I have a problem right now, I want to get the id of the newly inserted row of a user once the user asks a question and I will also like to pass the id to another page and display then question. What I really want is something similar with this stackoverflow when you post a question it take to another page and display it. Please how can I achieve this, any help is welcome. the id is auto increment

<?php
if(isset($_POST["title"]) && isset($_POST["question"]) ){
$title = $_POST["title"];
$ask = $_POST["question"];

$sql = $con->prepare("insert into question(userid,title,question)values(?,?,?)");
$sql->bind_param('iss',$userid,$title,$ask);
$sql->execute();
$sql->close();
  }?>

i just used the code post the question but i don't know what next.

Barmar
  • 741,623
  • 53
  • 500
  • 612
alert
  • 37
  • 4

1 Answers1

0

You can get it from:

From the manual for mysqli::$insert_id :

mysqli::$insert_id -- mysqli_insert_id — Returns the auto generated id used in the latest query

$sql->insert_id();

To pass on to other pages, you can set the last inserted ID to a session variable and get it later. One way is like this:

insert.php

<?php
    session_start();
    // Other codes

    $sql->execute();
    $_SESSION["last_id"] = $sql->insert_id();

And in the next file...

next.php

<?php
    session_start();
    // Other codes

    $sql->execute();
    $_SESSION["last_id"]; // has the previous inserted ID.
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252