0

in this code i have to insert some value in DB, then i have to take last id inserted in DB and sent it to another page to show the value of that id row. here is my code for insert data in DB :

try {
    $con = new PDO("mysql:host=localhost;dbname=resume", "root", "");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql_basic = "INSERT INTO user_basic_info (first_name,last_name,address,profile_pic,resume_file)
VALUES ('$FirstName','$LastName','$Address','$pic_destination','$resume_destination')";

    $con->exec($sql_basic);
    $last_id = $con->$lastInsertId();
} catch (PDOException $e) {
    echo $sql_basic . "<br>" . $e->getMessage() . "<br>";
}

$con = null;

header('Location: DB-Read.php?id=' . $last_id);

in another php page i have to take $last_id and use it. here is my code:

try {
    $user_id = $id;
    $conn = new PDO("mysql:host=localhost;dbname=resume", "root", "");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT first_name FROM user_basic_info where user_id = ".$user_id);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    echo "this is username: " . $result;

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$conn = null;

header('Location: show-edit.php?id=' . $last_id);

first of all $lastInsertId() not working !! after that, is my way true to take $last_id from first page ?

Kalagar
  • 379
  • 2
  • 6
  • 18
  • you can use `$_SESSION` – Hamid Jul 24 '16 at 05:53
  • Check the braces in the catch statement. This maybe causing an error message that you are not seeing. –  Jul 24 '16 at 05:55
  • You can set the last inserted id in SESSION['id'] and can retrieve it on next page and after usage you can unset this. SESSION is more secure way to pass database ids. – Afshan Shujat Jul 24 '16 at 06:31

2 Answers2

1

lastInsertId is a method of the PDO object. You need to call $conn->lastInsertId();.

Your given syntax $conn->$lastInsertId(); is valid, however, it does not do, what you are expecting. It considers $lastInsertId to be a variable containing the method name to be called. The following example would work as well:

$method_name = 'lastInsertId';
$conn->$method_name();

Be aware that your solution enables anyone to fetch any row of your table. Consider using sessions.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
-1

TRY THIS: use session:

try {
    $con = new PDO("mysql:host=localhost;dbname=resume", "root", "");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql_basic = "INSERT INTO user_basic_info    (first_name,last_name,address,profile_pic,resume_file)
VALUES     ('$FirstName','$LastName','$Address','$pic_destination','$resume_destination')";

   $con->exec($sql_basic);
   $_SESSION['id'] = $con->lastInsertId(); //it's a method
} catch (PDOException $e) {
    echo $sql_basic . "<br>" . $e->getMessage() . "<br>";
$con = null;

header('Location: DB-Read.php?id=' . $last_id);

Now use this code

try {
    $user_id = $_SESSION['id'];
    $conn = new PDO("mysql:host=localhost;dbname=resume", "root", "");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT first_name FROM user_basic_info where user_id = ".$user_id);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    echo "this is username: " . $result;

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$conn = null;

header('Location: show-edit.php?id=' . $last_id);
pankaj agarwal
  • 171
  • 2
  • 15
Md. Abdur Razzak
  • 35
  • 1
  • 1
  • 7