简体   繁体   中英

Why is mysqli_insert_id returning me 0

I actually read an answer here, the user says this happens when your table is not having any autoincrement column but am already having that, the other is connection is not established, but when I used this

if(!$running_db) {
    echo 'False';
} else {
    echo 'true'; //Returns me true so connection is establised
}

Than why it is returning me 0 ? Actually am inserting on page.php and after successfully inserted user is redirected to result.php page where I am echoing out the id using mysqli_insert_id so that I can highlight the field but it returns me 0 and the table row is thus failing to highlight

PS Am using procedural way

When you redirect to the new script you loose that information. Without seeing your code, all I can advice is to reat the mysqli_insert_id in the page.php script, save it in a SESSION variable and then redirect to result.php where you check that SESSION variable and find the saved id in it.

Note from the php manual :

http://php.net/manual/en/function.mysql-insert-id.php

"Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value. "

Retrieve the value on the same page, place it in session and then access the value from the session variable on any other page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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