473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Trouble returning id from auto increment

Could someone please help. I've searched alot of forums and website but none seem to give me the answer.

I've posted info to my database and i'm looking to return the id using the mysqli_insert_id() function. I'm posting it to php. Please someone help, going a bit mad as no integer is returned.

Expand|Select|Wrap|Line Numbers
  1. $query ="INSERT INTO New_Fault(Title,Forename,Surname,Email,Message)
  2. VALUES('$Title','$Forename','$Surname','$Email','$Message')";
  3.  
  4. $result = mysqli_query($cxn,$query) or die ('error making query');
  5. if($result){
  6. echo "Your unique Reference Number is = " mysqli_insert_id;
  7. echo "<BR>";
  8. echo "<a href=' put link here'>Back to main page</a>";
  9.     }
  10. else {echo "ERROR";
  11. }    
  12. mysqli_close();
  13. ?>
  14.  
Thanks G
Apr 23 '08 #1
10 3049
debasisdas
8,127 Expert 4TB
The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.

Note: Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() function will also modify the value returned by the mysqli_insert_id() function.

Return Values
The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

Note: If the number is greater than maximal int value, mysqli_insert_id() will return a string.
Apr 24 '08 #2
ronverdonk
4,258 Expert 4TB
The mysqli statement for this is mysqli_stmt_insert_id. However, there is no further description (yet) in the documenation, but it should be something like[php]mysqli_stmt_insert_id($result);[/php]If you are not sure you can always use the LAST_INSERT_ID() function of MySqli and your code would then be something like:[php]if($result){
$res=mysqli_query("SELECT LAST_INSERT_ID() FROM New_Fault AS last LIMIT 1");
$row=mysqli_fetch_row($res);
$last=$row[0];
echo "Your unique Reference Number is = $last <br>";
echo "<a href=' put link here'>Back to main page</a>";
}[/php]Ronald
Apr 24 '08 #3
The mysqli statement for this is mysqli_stmt_insert_id. However, there is no further description (yet) in the documenation, but it should be something like[php]mysqli_stmt_insert_id($result);[/php]If you are not sure you can always use the LAST_INSERT_ID() function of MySqli and your code would then be something like:[php]if($result){
$res=mysqli_query("SELECT LAST_INSERT_ID() FROM New_Fault AS last LIMIT 1");
$row=mysqli_fetch_row($res);
$last=$row[0];
echo "Your unique Reference Number is = $last <br>";
echo "<a href=' put link here'>Back to main page</a>";
}[/php]Ronald
Hi Ronald,

Thanks for your help but i must be doing something silly, when i implement your solution nothing is returned.

Any idea why?

G
Apr 24 '08 #4
ronverdonk
4,258 Expert 4TB
Hi Ronald,

Thanks for your help but i must be doing something silly, when i implement your solution nothing is returned.

Any idea why?
G
You mean that not even the ERROR message is returned? When not you must be doing something okay.

So: what is returned?

Ronald
Apr 24 '08 #5
You mean that not even the ERROR message is returned? When not you must be doing something okay.

So: what is returned?

Ronald
Hi Ronald,

No error message is returned because the information is sent correctly to the database, i check this manually.
The page displays;

Your unique Reference Number is =
Back to main page

No value is inserted after equals sign. It seems like its something small but I have no idea what.

G
Apr 25 '08 #6
ronverdonk
4,258 Expert 4TB
Theh let's have a look at the row. Do this and see what is displayed[php]if($result){
$res=mysqli_query("SELECT LAST_INSERT_ID() FROM New_Fault AS last LIMIT 1")
or die("Query error ".mysqli_error());
$row=mysqli_fetch_row($res);
echo '<pre>'; print_r($row);
$last=$row[0];
echo "Your unique Reference Number is = $last <br>";
echo "<a href=' put link here'>Back to main page</a>";
}[/php]Ronald
Apr 25 '08 #7
Theh let's have a look at the row. Do this and see what is displayed[php]if($result){
$res=mysqli_query("SELECT LAST_INSERT_ID() FROM New_Fault AS last LIMIT 1")
or die("Query error ".mysqli_error());
$row=mysqli_fetch_row($res);
echo '<pre>'; print_r($row);
$last=$row[0];
echo "Your unique Reference Number is = $last <br>";
echo "<a href=' put link here'>Back to main page</a>";
}[/php]Ronald

It returns Query error but no error message. This information is also still being posted to the database.

G
Apr 25 '08 #8
ronverdonk
4,258 Expert 4TB
I cannot see the problem. Last resort is to do this[php]$res=mysqli_query("SELECT LAST_INSERT_ID()");[/php] but that will only work when your ID is not a bigint.

Ronald
Apr 25 '08 #9
I cannot see the problem. Last resort is to do this[php]$res=mysqli_query("SELECT LAST_INSERT_ID()");[/php] but that will only work when your ID is not a bigint.

Ronald
It seems like a very trivial problem. Thanks for all your help in trying to get this sorted.

My table uses auto increment, is value for this is BIGINT?

Can this be changed so ur last suggestion can be tried?

G
Apr 25 '08 #10
ronverdonk
4,258 Expert 4TB
When your auto_increment field is a bigint you can change that to an int via
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE CHANGE COLUMN col_name int
When it is not or no longer a bigint, you can try that last suggestion.

Ronald
Apr 25 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Sugapablo | last post by:
Let's say I create a new record in a table like this: mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn); ....that had an auto-incrementing, unique identifying column named "ID"...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
1
by: don | last post by:
I'm trying to import a comma delimited text file into MS Access 2002 version - the first column is set for an auto increment primary key and everything works right the first time I import a text...
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
5
by: Paulovič Michal | last post by:
hi all, I have problem with SERIAL field type (or sequence functionality). I have table with three columns - ID, IDS, NAME. I want auto-increment IDS grouped by ID. Example: 1, 1, Ferdo 1, 2,...
5
by: vul | last post by:
In VB6 there is Auto Increment check box in Project Properties, which allow you have a new version every time you compile the project. Is there any easy way to have this feature in VB 2005? Some...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
5
by: David Wright | last post by:
Hello Everyone I would be grateful if someone could help me with the automatic increment of a field on my subform called ‘Test_SrNo’. I am Using Microsoft Office 2000. The auto entry of the...
13
by: BobLewiston | last post by:
Using Visual C# 2008 Express and SQL Server 2008 Express, I would like to insert new records into database "AdventureWorks", table "Person.Contact". To my surprise, this table's int-value identity...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.