简体   繁体   English

PHP无法获取插入的最后一条记录的ID

[英]PHP Can't get the ID of Last Record Inserted

I'm trying to get the ID of the last record inserted in my phpMyAdmin table, but I only get 0's so far. 我正在尝试获取插入到phpMyAdmin表中的最后一条记录的ID,但到目前为止我只得到0。

I have tried almost everything (for example the solution on this answer: a link or what this page recommend : a link ) but I can't get it working. 我几乎尝试了所有方法(例如,此答案的解决方案: 链接或此页面推荐的内容: link ),但我无法使其正常运行。

My table has a column id that is the primary key and is marked as AUTO_INCREMENT. 我的表具有作为主键的列ID,并标记为AUTO_INCREMENT。

I use a function query that make the connection to the database and then execute the queries: 我使用一个函数查询来建立与数据库的连接,然后执行查询:

function query($sql) {
    $conn = mysqli_connect ( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
    // Check connection
    if (mysqli_connect_errno ()) {
        echo 'connection failed: ' . mysqli_connect_error ();
    }
    // Check if the server is alive
    if (mysqli_ping ( $conn )) {
        // echo 'Connection is ok';
    } else {
        echo 'Error: ' . mysqli_error ( $dbc );
    }

    $sql = mysqli_query ( $conn, $sql );
    $last_id = mysqli_insert_id( $conn );
    $num_rows = mysqli_num_rows ( $sql );
    $result = mysqli_fetch_assoc ( $sql );

    return array (
            "num_rows" => $num_rows,
            "result" => $result,
            "sql" => $sql,
            "last_id" => $last_id
    );
    mysqli_close($conn);
}

Then I have another function from where I called the first one to execute the query: 然后,我从另一个调用第一个函数的位置执行查询:

function reg_shp_add($address_type, $company_name, $country, $state, $city, $zip, $street_name, $street_number, $tel){
    if($address_type=="residential"){
        $my_address_type = "r";
    }else{
        $my_address_type = "c";
    }
        $this->query ( "INSERT INTO `Addresses` (`company_name`, `street_address`, `address_two`, `zip_code`, `city_name`, `state_id`, `country_id`, `phone_number`, `res_comm_add`, `date_entered`) VALUES ('$company_name', '$street_name', '$street_number', '$zip', '$city', '$state', '$country', '$tel', '$my_address_type', CURRENT_TIMESTAMP)" );
        $address_id = $sql ['user_id'];
        $customer_id = $_COOKIE['userId'];
        $this->query ( "INSERT INTO `Cust_address_type` (`cust_id`, ` mail_address_id`, `address_type`) VALUES ('$customer_id', '$address_id', 'Shipping')" );
        return 'Address was saved.';

}

I first tried to retrieve the last id from my reg_shp_add function, but I guess it must be done from the first one. 我首先尝试从reg_shp_add函数中检索最后一个ID,但是我想它必须从第一个ID中完成。

Can anybody help me please? 有人可以帮我吗?

I test your code and the function mysqli_insert_id work correctly with me ( ["last_id"]=> int(8) ) . 我测试了您的代码,并且函数mysqli_insert_id与我正常工作了[[“ last_id”] => int(8)) But the problem is in your second function, on the data retrieval ; 但是问题出在您的第二个功能上,即数据检索上。

$this->query ( "INSERT INTO `Addresses` (`company_name`, `street_address`, `address_two`, `zip_code`, `city_name`, `state_id`, `country_id`, `phone_number`, `res_comm_add`, `date_entered`) VALUES ('$company_name', '$street_name', '$street_number', '$zip', '$city', '$state', '$country', '$tel', '$my_address_type', CURRENT_TIMESTAMP)" );
$address_id = $sql ['user_id'];

Here the $sql variable is undefined, you miss to store the return value into $sql , like that : 这里的$ sql变量是未定义的,您会想将返回值存储到$ sql中 ,就像这样:

$sql = $this->query ( "INSERT INTO `Addresses` (`company_name`, `street_address`, `address_two`, `zip_code`, `city_name`, `state_id`, `country_id`, `phone_number`, `res_comm_add`, `date_entered`) VALUES ('$company_name', '$street_name', '$street_number', '$zip', '$city', '$state', '$country', '$tel', '$my_address_type', CURRENT_TIMESTAMP)" );
$address_id = $sql ['user_id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM