0

I have created a registration form which is in application.php and when submitted it is processed and the contents are stored using submit.php and I want to redirect to thanks.php with a parameter called message with value equal to the primay key of the recently inserted row.. For example it should return my 100th applicant to the page,

www.mydomain.com/thanks.php?message=100

where 100 is the Primary key (AI).

In other words I want to print the primary key of the table as a refernce id for the users.. I tried

header("location: thanks.php?message=".$id); and

header("location: thanks.php?message=".$_POST['id']);

Both dont work for me.

Kindly help me guys!

EDIT:

require("admin/sources/connection.php");

$id = mysql_insert_id();

// Other variables are declared here

$sql = "my_INSERT_query_goes_here";

$result = $conn->query($sql);

if($result) {

    header("location: thanks.php?message=".$id);
}

This is my submit.php code

4

2 回答 2

1

错误的执行顺序 - 简单的修复

您首先分配 $id,然后运行查询。

扭转它们以使其工作。

不使用自动增量?

我在您评论中的查询中看到,您手动传递了要输入的 ID。您是否意识到这mysql_insert_id()不会返回您调用“id”的 sql 列,而是您设置自动增量的列?这是表中的单行,它会自动获取递增的数字,而无需手动输入。

于 2013-06-07T17:53:54.317 回答
0

几个问题......看起来你正在混合mysql_方法mysqli并且你的执行顺序错误。

如果您的数据库表是自动递增的,则无需在查询中传递 ID。通过在查询中调用$id = mysql_insert_id();和使用 id,您将传入 0 作为使用下一个自动增量 id 的 id。

获取insert_idafter$conn->query($sql);将返回给您使用的插入 ID。

回显一些变量以查看您在mysql_insert_id();之前和之后获得的值。

但是,如果您正在做 a $conn->query(),那么您就没有使用标准mysql_功能。

尝试这个:

require "admin/sources/connection.php";

// Other variables are declared here

$sql = "my_INSERT_query_goes_here";

$result = $conn->query($sql);

if($result) {
    $id = $conn->insert_id;
    header("location: thanks.php?message=".$id);
}
于 2013-06-07T18:05:06.823 回答