Php 将用户发送到页面
我正在使用PHP脚本将数据发送到我的数据库 这是我的剧本:Php 将用户发送到页面,php,Php,我正在使用PHP脚本将数据发送到我的数据库 这是我的剧本: <?php session_start(); $correct = true; $_SESSION['user_id']; $firstname = $_POST['firstname'] ; $lastname = $_POST['lastname'] ; if($correct){ $db = new PDO('mysql:host=localhost;dbname=database',
<?php
session_start();
$correct = true;
$_SESSION['user_id'];
$firstname = $_POST['firstname'] ;
$lastname = $_POST['lastname'] ;
if($correct){
$db = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$query = "INSERT INTO names(user_id, firstname, lastname) VALUES (?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->execute(array($_SESSION['user_id'], $firstname, $lastname));
header('Location: ../names.php');
}
else
{
echo "<p>Error</p>";
}
?>
脚本工作正常,但是在执行SQL语句之后,我想将用户发送到一个页面,在该页面上用户可以看到他输入的数据
在当前状态下,脚本将用户发送到names.php
。我想将用户发送到names.php?id=8
id
列是自动递增的
有人知道我如何将用户发送到已执行语句的
id
?执行以下操作后:
header('Location: ../names.php?id='.$db->lastInsertId);
在您完成任务后,请执行以下操作:
header('Location: ../names.php?id='.$db->lastInsertId);
您可以通过此
$id = $db->lastInsertId();
然后,您可以将其传递给标题
方法。如下图所示
header('Location: ../names.php?id='.$id);
您可以通过此命令获取最后插入的id
$id = $db->lastInsertId();
然后,您可以将其传递给标题
方法。如下图所示
header('Location: ../names.php?id='.$id);
您可以尝试从Insert语句之后的user\U id=$\U SESSION['user\U id']表中对Select id执行另一个查询
然后您可以在链接名上使用result。php?id=[result from Select]您可以尝试在Insert语句之后从user\u id=$\u SESSION['user\u id']所在的表中对Select id执行另一个查询
然后您可以在链接名上使用result.php?id=[result from Select]我猜您的意思是
header('Location:../names.php?id='。$db->lastInsertId)
我猜您的意思是标题('Location:../names.php?id='.$db->lastInsertId)
您忘记了字符串连接的
。:)您忘记了字符串连接的
。:)