【问题标题】:sent last id inserted variable to another page in php [duplicate]在php中将最后一个id插入变量发送到另一个页面[重复]
【发布时间】:2016-07-24 05:52:14
【问题描述】:

在这段代码中,我必须在 DB 中插入一些值,然后我必须将最后一个 id 插入 DB 并将其发送到另一个页面以显示该 id 行的值。这是我在数据库中插入数据的代码:

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

在另一个 php 页面中,我必须使用 $last_id 并使用它。这是我的代码:

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

首先$lastInsertId() 不工作!! 之后,我从第一页获取$last_id 的方式是否正确?

【问题讨论】:

  • 你可以使用$_SESSION
  • 检查 catch 语句中的大括号。这可能会导致您没有看到的错误消息。
  • 您可以在 SESSION['id'] 中设置最后插入的 id,并可以在下一页检索它,使用后您可以取消设置。 SESSION 是传递数据库 ID 的更安全的方式。

标签: php mysql pdo last-insert-id


【解决方案1】:

lastInsertIdPDO 对象的一个​​方法。您需要致电$conn-&gt;lastInsertId();

您给定的语法$conn-&gt;$lastInsertId(); 是有效的,但是,它并没有达到您的预期。它认为$lastInsertId 是一个包含要调用的方法名称的变量。以下示例也可以:

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

请注意,您的解决方案使任何人都可以获取表格的任何行。考虑使用会话。

【讨论】:

    【解决方案2】:

    试试这个: 使用会话:

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

    现在使用此代码

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

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多