donglu9743 2016-07-24 05:52
浏览 14

将最后一个id插入变量发送到php中的另一个页面[复制]

This question already has an answer here:

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 ?

</div>
  • 写回答

2条回答 默认 最新

  • duanrongshi1544 2016-07-24 06:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。