有什么办法让我从选定的行中选择最后插入的ID。 (并非来自表中的所有行)下面是我编写的代码。我想选择一个具有最高周属性值的属性。那也是该选定集合的最后插入的ID。

$get_payment_details = 'SELECT * FROM payment WHERE m_id="' . $mselcted_memberI . '" AND app_id= "' . $aprvd_appid . '"';

$get_payment = mysql_query($get_payment_details);
$get_pay_result = mysql_fetch_assoc($get_payment);
$get_pay_count = mysql_num_rows($get_payment);

if ($get_pay_count < 1) {
    $query1 = "INSERT INTO payment(date,m_id,app_id,week,capital_topay,interest_to_pay,received,advanced,remain_capital,remain_interest)
               VALUES('$date1',"."'".$mselcted_memberI."',"."'".$aprvd_appid."',"."'".$week1."',"."'".$aprvd_amount."',"."'".$ttl_interest."',"."'".$received1."',"."'".$advanced1."',"."'".$aprvd_amount."',"."'".$ttl_interest."')";
    $login_set = mysql_query($query1,$connection);
    confirm_query($login_set);
    echo $week1;
} else if ($get_pay_count >0){

}

最佳答案

如果使用PHP 5.5之前的版本,则可以使用mysql_insert_id()检索最后插入的ID。从文档中:

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>


但是请注意,自PHP 5.5.0起不推荐使用此函数。规范推荐的解决方案如下:


MySQLi:使用mysqli_insert_id()
PDO_MySQL:使用PDO::lastInsertId()

09-12 14:46