如何在PHP中获取最后插入的id

时间:2015-11-02 06:21:40

标签: php mysql

这里我试图从我拥有product_id的表中获取MySQL数据库中插入的id。插入后我想获取最新插入的product_id并将其存储在数组['newid'][]

插入查询非常好,但我无法将product_id输入到数组中。当我打印数组时,我得到NULL值。

 $link = mysqli_connect(db_host,db_user,db_password,db_name);    
    if (condition) {
    $sqlin = "INSERT INTO product_list (product_name, product_category, product_price,product_description,product_sharing_basis,product_co_owners,walden_product_price,product_referrence_URL,product_proposed_user_id,product_image_url,product_refurbish_factor,product_insurance_factor,product_life,product_size_category,product_publish_status) VALUES ('$product_name', '$product_category', '$product_price', '$product_description', '$share_basis', '$co_owners', '$walden_product_price', '$pro_url', '$proposed_by','files/uploaded_images/".$_FILES['file']['name']."', '$refurbishment_factor', '$insurance_factor', '$product_life', '$size_category','$approve')";          
    } else {
    $sqlin = "INSERT INTO product_list (product_name, product_category, product_price,product_description,product_sharing_basis,product_co_owners,walden_product_price,product_referrence_URL,product_proposed_user_id,product_image_url,product_refurbish_factor,product_insurance_factor,product_life,product_size_category) VALUES ('$product_name', '$product_category', '$product_price', '$product_description', '$share_basis', '$co_owners', '$walden_product_price', '$pro_url', '$proposed_by','files/uploaded_images/".$_FILES['file']['name']."', '$refurbishment_factor', '$insurance_factor', '$product_life', '$size_category')";
    }
    if(mysqli_query($link, $sqlin)){    
        $newisid = mysqli_insert_id($link);
        $_SESSION['newid'][] = $newisid;

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

php.net:$ mysqli-> insert_id

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

答案 1 :(得分:0)

尝试使用mysql_insert_id()获取以前插入的ID。

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);//previously insert id here.

} else {
    //error
}

请参阅thisthis

修改

您也可以使用LAST_INSERT_ID()。检查官方mysql doc

答案 2 :(得分:0)

试试这个:

$link = mysqli_connect(db_host,db_user,db_password,db_name);

if (condition) {
    $sqlin = "INSERT INTO product_list (product_name, product_category, product_price,product_description,product_sharing_basis,product_co_owners,walden_product_price,product_referrence_URL,product_proposed_user_id,product_image_url,product_refurbish_factor,product_insurance_factor,product_life,product_size_category,product_publish_status) VALUES ('$product_name', '$product_category', '$product_price', '$product_description', '$share_basis', '$co_owners', '$walden_product_price', '$pro_url', '$proposed_by','files/uploaded_images/".$_FILES['file']['name']."', '$refurbishment_factor', '$insurance_factor', '$product_life', '$size_category','$approve')";          
} else {
    $sqlin = "INSERT INTO product_list (product_name, product_category, product_price,product_description,product_sharing_basis,product_co_owners,walden_product_price,product_referrence_URL,product_proposed_user_id,product_image_url,product_refurbish_factor,product_insurance_factor,product_life,product_size_category) VALUES ('$product_name', '$product_category', '$product_price', '$product_description', '$share_basis', '$co_owners', '$walden_product_price', '$pro_url', '$proposed_by','files/uploaded_images/".$_FILES['file']['name']."', '$refurbishment_factor', '$insurance_factor', '$product_life', '$size_category')";
}


if($link->query($sqlin)){
    $newisid = $link->insert_id;
    $_SESSION['newid'][] = $newisid;
}

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