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

P.S Am using procedural way

Acidic Cloud
  • 597
  • 1
  • 7
  • 15
  • 1
    You need to show us your `INSERT` query, as well as your call to `mysql_insert_id`, and it's also probably helpful to include the output of `SHOW CREATE TABLE tablename` so we can see what the structure of your table is. But without any of that - have you made sure the data is actually getting to your table without errors? – Colin M Feb 11 '13 at 14:18
  • Post you db-related code here! – rkosegi Feb 11 '13 at 14:18
  • 1
    result.php is a completely new script, that has no knowledge of what has occurred in the page.php script.... mysqli_insert_id() is only aware of inserts that have taken place in the script where it is called.... call mysqli_insert_id() in page.php, and pass the returned value to result.php – Mark Baker Feb 11 '13 at 14:19
  • refer Marcus' answer's 2nd point [here](http://stackoverflow.com/questions/8243503/mysql-insert-id-returns-0). – Bhavik Shah Feb 11 '13 at 14:19
  • @ColinMorelli The loop is dynamic, I decide the rows and column dynamically, table is perfect, insert works perfectly, no errors but `mysql_insert_id` returns me `0` – Acidic Cloud Feb 11 '13 at 14:19
  • @MarkBaker So should I save the data in a session and destroy it later? – Acidic Cloud Feb 11 '13 at 14:20
  • @AcidicCloud Didn't even catch that part of the question. Insert IDs are tracked on a per-connection basis and each new page load establishes a new connection. This means that after opening `result.php` the insert ID is no longer available. – Colin M Feb 11 '13 at 14:21
  • @Acidic - session is probably the safest way of passing the result – Mark Baker Feb 11 '13 at 14:21
  • @ColinMorelli Cool, so I store the value in a session and unset it after use right? – Acidic Cloud Feb 11 '13 at 14:21
  • @MarkBaker Thank you very much, will use it for highlight, and will unset it after I've used it :) – Acidic Cloud Feb 11 '13 at 14:22
  • show us your `$running_db` how is defined – echo_Me Feb 11 '13 at 14:27
  • @echo_me party's over ;) – Acidic Cloud Feb 11 '13 at 14:31

2 Answers2

2

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.

Eggplant
  • 1,903
  • 1
  • 14
  • 24
  • Ya, actually I decided the solution by reading comments :) thanks anyways will mark the answer as correct as soon as system allows me – Acidic Cloud Feb 11 '13 at 14:23
2

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.

Palak Taneja
  • 1,983
  • 3
  • 13
  • 12