473,396 Members | 2,057 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,396 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 20 '05 #1
3 1315
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 20 '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 20 '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 20 '05 #4

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

Similar topics

6
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...
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...
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...
0
by: polocar | last post by:
Hi, I have noticed a strange behaviour of CurrencyManager objects in C# (I use Visual Studio 2005 Professional Edition). Suppose that you have a SQL Server database with 2 tables called "Cities"...
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.