2015-11-02 106 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

相关问题