从MySQL的最后一个条目获取主要内容并通过链接中的电子邮件传递

时间:2022-09-25 16:13:14

I have a PHP script which is supposed to send an email with a link which can be clicked to alter the last entry in the MySQL database. To do this I'm trying to include the primary key as a variable in the link but the variable is coming up blank or possibly just not sending correctly. I've never done this before so not sure what the problem is, probably something stupid but silly me has been stumped for a while.

我有一个PHP脚本,它应该发送一个带有链接的电子邮件,可以单击该链接来更改MySQL数据库中的最后一个条目。为此,我试图将主键作为变量包含在链接中,但变量是空白的,或者可能只是没有正确发送。我以前从来没有这样做过,所以不确定问题是什么,可能是愚蠢的事,但是愚蠢的我已经被困了一段时间了。

The link in the email currently ends like this: feedback/approve/?id=

目前电子邮件中的链接如下所示:feedback / approve /?id =

Here is the code, if you need more info let me know:

这是代码,如果您需要更多信息,请告诉我:

<?PHP
error_reporting(E_ALL);
require ('config.php');
require ('index.php');
/* Connection */
$table = "FeedbackWRW";
$conn = new mysqli (host, user, pass, db);
$sql = "INSERT INTO $table (answer1, answer2, answer3, answer4, name, detail1, detail3, title) VALUES ('$values')";
$primary = mysqli_fetch_row(mysqli_query($conn,"SELECT LAST(`primary`) FROM $table"));
$approvallink = "${clienturl}feedback/approve/?id=" . "$primary";

if(mysqli_query($conn, $sql)
&& 
mail("$testemail",'Feedback Entry',
"
Satisfied customer: $answer1 
Would recommend: $answer2 
Testimonial: $answer3
Suggestion box: $answer4
Name: $name
Email: $detail1
Newsletter Opt-in: $detail3

To approve this entry, please click this link or paste it into your browser window:
$approvallink


","From: $clientemail
Reply-To: $detail2
Return-Path: $detail2
X-Mailer: PHP
CC: 
BCC: 
")



)
{header('Location: thankyou.php');
exit();
}

else { echo "Error: " . $sql . "<br>" . mysqli_error($conn);}


?>

3 个解决方案

#1


You didn't include the table definition, I assume you have a "primary" column defined as auto-incremental. Double check your table and make sure it has non-empty values.

您没有包含表定义,我假设您有一个“主”列定义为自动增量。仔细检查您的表并确保它具有非空值。

then use max(...) instead last(...) in your query to retrieve the last id inserted.

然后在查询中使用max(...)而不是last(...)来检索插入的最后一个id。

#2


Try $primary=$mysqli->insert_id; to get the last inserted.It will return the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute.

试试$ primary = $ mysqli-> insert_id;获取最后一次插入。它将在具有AUTO_INCREMENT属性的列的表上返回查询生成的ID。

Also,make sure this is execeuted after your query. Currently you are fetching your $primary before the execution of query. That is why you are getting it as blank.

此外,请确保在查询后执行此操作。目前,您在执行查询之前获取$ primary。这就是为什么你把它变成空白的原因。

You may give a try with this code(NOT TESTED)

您可以尝试使用此代码(未测试)

    <?PHP
    error_reporting(E_ALL);
    require ('config.php');
    require ('index.php');
    /* Connection */
    $table = "FeedbackWRW";
    $conn = new mysqli (host, user, pass, db);
    $sql = "INSERT INTO $table (answer1, answer2, answer3, answer4, name, detail1, detail3, title) VALUES ('$values')";
    mysqli_query($conn, $sql);
    $primary = $mysqli->insert_id;
    $approvallink = "${clienturl}feedback/approve/?id=" . "$primary";
    if($primary !=''
    && 
    mail("$testemail",'Feedback Entry',
    "
    Satisfied customer: $answer1 
    Would recommend: $answer2 
    Testimonial: $answer3
    Suggestion box: $answer4
    Name: $name
    Email: $detail1
    Newsletter Opt-in: $detail3

    To approve this entry, please click this link or paste it into your browser window:
    $approvallink


    ","From: $clientemail
    Reply-To: $detail2
    Return-Path: $detail2
    X-Mailer: PHP
    CC: 
    BCC: 
    ")



    )
    {header('Location: thankyou.php');
    exit();
    }

    else { echo "Error: " . $sql . "<br>" . mysqli_error($conn);}


    ?>

http://php.net/manual/en/mysqli.insert-id.php

#3


You can use DESC clause in the ORDER BY and LIMIT 1. That will also give you last record.

您可以在ORDER BY和LIMIT 1中使用DESC子句。这也将为您提供最后一条记录。

More information: Select last row in MySQL

更多信息:选择MySQL中的最后一行

#1


You didn't include the table definition, I assume you have a "primary" column defined as auto-incremental. Double check your table and make sure it has non-empty values.

您没有包含表定义,我假设您有一个“主”列定义为自动增量。仔细检查您的表并确保它具有非空值。

then use max(...) instead last(...) in your query to retrieve the last id inserted.

然后在查询中使用max(...)而不是last(...)来检索插入的最后一个id。

#2


Try $primary=$mysqli->insert_id; to get the last inserted.It will return the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute.

试试$ primary = $ mysqli-> insert_id;获取最后一次插入。它将在具有AUTO_INCREMENT属性的列的表上返回查询生成的ID。

Also,make sure this is execeuted after your query. Currently you are fetching your $primary before the execution of query. That is why you are getting it as blank.

此外,请确保在查询后执行此操作。目前,您在执行查询之前获取$ primary。这就是为什么你把它变成空白的原因。

You may give a try with this code(NOT TESTED)

您可以尝试使用此代码(未测试)

    <?PHP
    error_reporting(E_ALL);
    require ('config.php');
    require ('index.php');
    /* Connection */
    $table = "FeedbackWRW";
    $conn = new mysqli (host, user, pass, db);
    $sql = "INSERT INTO $table (answer1, answer2, answer3, answer4, name, detail1, detail3, title) VALUES ('$values')";
    mysqli_query($conn, $sql);
    $primary = $mysqli->insert_id;
    $approvallink = "${clienturl}feedback/approve/?id=" . "$primary";
    if($primary !=''
    && 
    mail("$testemail",'Feedback Entry',
    "
    Satisfied customer: $answer1 
    Would recommend: $answer2 
    Testimonial: $answer3
    Suggestion box: $answer4
    Name: $name
    Email: $detail1
    Newsletter Opt-in: $detail3

    To approve this entry, please click this link or paste it into your browser window:
    $approvallink


    ","From: $clientemail
    Reply-To: $detail2
    Return-Path: $detail2
    X-Mailer: PHP
    CC: 
    BCC: 
    ")



    )
    {header('Location: thankyou.php');
    exit();
    }

    else { echo "Error: " . $sql . "<br>" . mysqli_error($conn);}


    ?>

http://php.net/manual/en/mysqli.insert-id.php

#3


You can use DESC clause in the ORDER BY and LIMIT 1. That will also give you last record.

您可以在ORDER BY和LIMIT 1中使用DESC子句。这也将为您提供最后一条记录。

More information: Select last row in MySQL

更多信息:选择MySQL中的最后一行