473,383 Members | 1,859 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,383 software developers and data experts.

How can I reference ID during an INSERT?


Gentle People,

I think I read some where on how to do this, maybe I'm wrong.

I'm working on someone elses PHP code which updates an existing database
- One of the programmers used to generate a unique key (a transaction
code) for every purchase using a hash - A second programmer thought the
idea of using the unique numeric ID field, prefixed with the two letters
'Tx' would be more sensible. He did this by first making an insert to
the database with whatever transaction details he had, then getting the
mysql_insert_id() and then going back and updateing the exact record he
just inserted except to modify one field/column with the value he got
from his mysql_insert_id() prefixed with the letters "Tx".

I am pretty sure I can do this all in one go and I'd appreciate if
anyone can help direct me here...

basically, I want to do something like

mysql> insert myTable set firstname="elvis",txcode='tx'+ID;

Where ID is the ID of the new/active record being inserted.

The insert works, except txcode equals zero - when the unique ID for the
row is/was 2795 - and it does not even have a 'tx' prefix which leads me
to believe its performing some maths to my request.

Can what I want to do be done in a single step, or must I go back and
update the record after the insert?

All help, via the newsgroup please (to help others) is greatly appreciated,

thanks
randelld
Jul 17 '05 #1
6 1844
Reply-Via-Newsgroup Thanks wrote:
(snip)
basically, I want to do something like

mysql> insert myTable set firstname="elvis",txcode='tx'+ID;

Where ID is the ID of the new/active record being inserted.


[ partial non-answer ]
Why? (Yes, I know you are changing someone else's code)

Isn't if *far* better to /not/ duplicate information in the database?
Instead of Isn't it better to have
id | txcode | etc id | txcode | etc
----+--------+----- ----+--------+-----
17 | tx17 | ... 17 | tx | ...
71 | tx71 | ... 71 | tx | ...

You can always "select concat(txcode, id)" from the table on the right
to obtain "tx17" and "tx71"
And, if the column txcode is filled with "tx" for all lines, simply
remove it from the database :)
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
Pedro Graca wrote:
Reply-Via-Newsgroup Thanks wrote:
(snip)
basically, I want to do something like

mysql> insert myTable set firstname="elvis",txcode='tx'+ID;

Where ID is the ID of the new/active record being inserted.

[ partial non-answer ]
Why? (Yes, I know you are changing someone else's code)

Isn't if *far* better to /not/ duplicate information in the database?
Instead of Isn't it better to have
id | txcode | etc id | txcode | etc
----+--------+----- ----+--------+-----
17 | tx17 | ... 17 | tx | ...
71 | tx71 | ... 71 | tx | ...

You can always "select concat(txcode, id)" from the table on the right
to obtain "tx17" and "tx71"
And, if the column txcode is filled with "tx" for all lines, simply
remove it from the database :)


Everything you've said is very true and something I've already
considered - However there are limits being imposed on the bounds of my
project which I must follow - I am not permitted to edit other code that
depends on this txcode - Thus, while your suggestion is valid and
helpful, and to all accounts, simple to implement - if I were to use
your suggestion, txcode would no longer be unique and therefore make
other dependancies fail.

Therefore, have you (or anyone else) any idea on how I can actively copy
the id column to the txcode column and prefix it with 'tx' ?

Thanks in advance,
randell d.
Jul 17 '05 #3
Reply-Via-Newsgroup Thanks wrote:

Gentle People,

I think I read some where on how to do this, maybe I'm wrong.

I'm working on someone elses PHP code which updates an existing database
- One of the programmers used to generate a unique key (a transaction
code) for every purchase using a hash - A second programmer thought the
idea of using the unique numeric ID field, prefixed with the two letters
'Tx' would be more sensible. He did this by first making an insert to
the database with whatever transaction details he had, then getting the
mysql_insert_id() and then going back and updateing the exact record he
just inserted except to modify one field/column with the value he got
from his mysql_insert_id() prefixed with the letters "Tx".

I am pretty sure I can do this all in one go and I'd appreciate if
anyone can help direct me here...

basically, I want to do something like

mysql> insert myTable set firstname="elvis",txcode='tx'+ID;

Where ID is the ID of the new/active record being inserted.

The insert works, except txcode equals zero - when the unique ID for the
row is/was 2795 - and it does not even have a 'tx' prefix which leads me
to believe its performing some maths to my request.

Can what I want to do be done in a single step, or must I go back and
update the record after the insert?

All help, via the newsgroup please (to help others) is greatly appreciated,

thanks
randelld


I had the same problem some time ago.. As far as I remember, I got rid
of it by sending a query to the db:

$result = mysql_query("last_insert_id()");
$row = mysql_fetch_row($result);
$lastId = $row[0];

For some reason the php's function returns 0..
Jul 17 '05 #4
Example:

$user_id is an int, autoincremented column on my table...

$query = "insert blah blah blah..." ;

$result = mysql_query( $query, $connection ) or die ( "Error in query: $query: "
.. mysql_error());

$user_id = mysql_insert_id() ;
-DG-
Lüpher Cypher wrote:
Reply-Via-Newsgroup Thanks wrote:

Gentle People,

I think I read some where on how to do this, maybe I'm wrong.

I'm working on someone elses PHP code which updates an existing
database - One of the programmers used to generate a unique key (a
transaction code) for every purchase using a hash - A second
programmer thought the idea of using the unique numeric ID field,
prefixed with the two letters 'Tx' would be more sensible. He did
this by first making an insert to the database with whatever
transaction details he had, then getting the mysql_insert_id() and
then going back and updateing the exact record he just inserted except
to modify one field/column with the value he got from his
mysql_insert_id() prefixed with the letters "Tx".

I am pretty sure I can do this all in one go and I'd appreciate if
anyone can help direct me here...

basically, I want to do something like

mysql> insert myTable set firstname="elvis",txcode='tx'+ID;

Where ID is the ID of the new/active record being inserted.

The insert works, except txcode equals zero - when the unique ID for
the row is/was 2795 - and it does not even have a 'tx' prefix which
leads me to believe its performing some maths to my request.

Can what I want to do be done in a single step, or must I go back and
update the record after the insert?

All help, via the newsgroup please (to help others) is greatly
appreciated,

thanks
randelld

I had the same problem some time ago.. As far as I remember, I got rid
of it by sending a query to the db:

$result = mysql_query("last_insert_id()");
$row = mysql_fetch_row($result);
$lastId = $row[0];

For some reason the php's function returns 0..


Jul 17 '05 #5
Data Goob wrote:
Example:

$user_id is an int, autoincremented column on my table...

$query = "insert blah blah blah..." ;

$result = mysql_query( $query, $connection ) or die ( "Error in query:
$query: " .. mysql_error());

$user_id = mysql_insert_id() ;


Try to change
$user_id = mysql_insert_id();
to
$result = mysql_query("last_insert_id()");
$row = mysql_fetch_row($result);
$user_id = $row[0];

If that works, I'd simply wrap it as a function.
Jul 17 '05 #6
So writing 3 extra lineths of code is better than what I thuggested.

Thankths Lupher.

Lüpher Cypher wrote:
Data Goob wrote:
Example:

$user_id is an int, autoincremented column on my table...

$query = "insert blah blah blah..." ;

$result = mysql_query( $query, $connection ) or die ( "Error in query:
$query: " .. mysql_error());

$user_id = mysql_insert_id() ;


Try to change
$user_id = mysql_insert_id();
to
$result = mysql_query("last_insert_id()");
$row = mysql_fetch_row($result);
$user_id = $row[0];

If that works, I'd simply wrap it as a function.


Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
3
by: Reply-Via-Newsgroup Thanks | last post by:
Gentle People, I think I read some where on how to do this, maybe I'm wrong. I'm working on someone elses PHP code which updates an existing database - One of the programmers used to generate...
1
by: tonci.tomic | last post by:
I have windows service running on win2000 and client applications on local network connected to service via remoting. Service acts as interface to MSSQL 2000 database and it uses Microsoft Data...
7
by: kon george | last post by:
Can somebody assist. I build this code on a dev laptop and copied across the entire code to a Windows 2003 server with 1.1 framework. It is basic ASP.NE T that uses web service for SQL Server...
3
by: rdraider | last post by:
I'm doing a data conversion project, moving data from one SQL app to another. I'm using INSERT INTO with Select and have the syntax correct. But when executing the script I get: Server: Msg...
4
by: Xela | last post by:
Hi I am facing the following problem. I load a fact table with around 25 millons lines, and 7 indexes. I load it with 3 million line subsets. I am on a quadriprocessor Solaris machine. The...
8
by: Charlie J | last post by:
I have a real stumper. I added a server side table to a home page that has two other server side tables on it that have been working great. In Visual Studio everything works great. When I put...
2
by: puzzlecracker | last post by:
Does map copy the object, both key and value during the insert? is this example correct: std::map<std::string Tmy_map; template<typename T> void foo(const std::string str, const T & t){...
5
by: dotnetnovice | last post by:
Hi everybody actually i was trying to insert some records in to SQL server through C# Wndows Aplication and during that i face an error "Object reference not set to an instance of an object." and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.