ThePsion5 Posted May 5, 2006 Share Posted May 5, 2006 Hi Guys,I'm writing a PHP function that inserts multiple values into a MySQL database, and then creates a new entry in a different table that references the freshly inserted values. I can't remember how to do this, however. My insert statement looks something like this: INSERT INTO Clients VALUES(0, 'name','site'), VALUES(0, 'name2', 'site2'), VALUES ('name3', 'site3'); Should I break the queries into individual inserts and then create the reference using New.ID or is there a better way? Thanks in advance!-Sean Link to comment Share on other sites More sharing options...
reportingsjr Posted May 5, 2006 Share Posted May 5, 2006 You did it kind of wrong.. I would set it up this way: INSERT INTO `Clients` VALUES (0, 'name', 'site') VALUES (0, 'name2', 'site2') VALUES (0, 'name3', 'site3'); try that.. Link to comment Share on other sites More sharing options...
ThePsion5 Posted May 8, 2006 Author Share Posted May 8, 2006 You did it kind of wrong.. I would set it up this way:INSERT INTO `Clients` VALUES (0, 'name', 'site') VALUES (0, 'name2', 'site2') VALUES (0, 'name3', 'site3'); try that.. <{POST_SNAPBACK}> Hah, that does help, doesn't it? What I'm still wondering is if I can get the IDs of the newly inserted values without having to use a select statement to find them again. As in can I avoid having to use a statement like the following:SELECT ID FROM 'Clients' WHERE Name='name' AND site='site' OR Name='name2' AND site='site2' (and so on...) Is it possible for me to use NEW here, as in a much easier statement like this one: select ID from NEW Link to comment Share on other sites More sharing options...
aspnetguy Posted May 8, 2006 Share Posted May 8, 2006 you could do thisMS SQL SELECT TOP 3 * FROM Clients ORDER BY ID DESC MySql SELECT * FROM Clients ORDER BY ID DESC LIMIT 3 This only works if ID is unique and increments with each new entry. Link to comment Share on other sites More sharing options...
ThePsion5 Posted May 8, 2006 Author Share Posted May 8, 2006 you could do thisMS SQLSELECT TOP 3 * FROM Clients ORDER BY ID DESC MySql SELECT * FROM Clients ORDER BY ID DESC LIMIT 3 This only works if ID is unique and increments with each new entry. <{POST_SNAPBACK}> That works perfectly well, actually. Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 10, 2006 Share Posted May 10, 2006 Just as an aside, you can also use this function:http://us2.php.net/manual/en/function.mysql-insert-id.phpBut you would have to insert each value individually and execute this in a loop or something. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now