2015-11-02 149 views
0

這裏我試圖從我有product_id的表中的MySQL數據庫中獲取插入的標識。插入後,我想獲得最新插入的product_id並將其存儲在數組中['newid'][]如何獲取PHP中的最後一個插入的標識

插入查詢進行得相當不錯,但我無法將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; 

我該如何解決這個問題?

+0

你如何打印您陣列,並有啓動會議在你的網頁上? – Saty

+0

'「插入查詢進行得非常好」......但是你的英語語法卻很差勁。你的意思是說''進行得很好'。' –

+0

@Saty是的,我已經開始會話。我使用echo var_dump($ _ SESSION ['newid']);打印。 –

回答

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

+0

Mithun Raikar已經在他的代碼中使用了'mysqli_insert_id' – Suyog

+0

如果你回顯最後一個插入id @ mithun –

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); 
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

相關問題