Re: [PHP] RE: non-auto increment question
Jim Lucas wrote: PJ wrote: haliphax wrote: On Fri, Mar 13, 2009 at 1:24 PM, PJ af.gour...@videotron.ca wrote: Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book           ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers )         VALUES           ('$titleIN', '$sub_titleIN', '$descrIN',           '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN');     $result1 = mysql_query($sql1, $db);   $autoid = mysql_insert_id($result1);     $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN');       $result2 = mysql_query($sql2, $db);   $authorID = mysql_insert_id($result2);     $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1');       $result2a = mysql_query($sql2a, $db);     $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN');       $result2b = mysql_query($sql2b, $db);   $author2ID = mysql_insert_id($result2b);     $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2');       $result2c = mysql_query($sql2c, $db);     $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN');       $result3 = mysql_query($sql3, $db);   $publisherID = mysql_insert_id($result3);     $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' );       $result3a = mysql_query($sql3a, $db);    Â
Re: [PHP] RE: non-auto increment question
PJ wrote: Jim Lucas wrote: PJ wrote: haliphax wrote: On Fri, Mar 13, 2009 at 1:24 PM, PJ af.gour...@videotron.ca wrote: Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book           ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers )         VALUES           ('$titleIN', '$sub_titleIN', '$descrIN',           '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN');     $result1 = mysql_query($sql1, $db);   $autoid = mysql_insert_id($result1);     $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN');       $result2 = mysql_query($sql2, $db);   $authorID = mysql_insert_id($result2);     $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1');       $result2a = mysql_query($sql2a, $db);     $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN');       $result2b = mysql_query($sql2b, $db);   $author2ID = mysql_insert_id($result2b);     $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2');       $result2c = mysql_query($sql2c, $db);     $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN');       $result3 = mysql_query($sql3, $db);   $publisherID = mysql_insert_id($result3);     $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' );       $result3a = mysql_query($sql3a, $db);     foreach($_POST['categoriesIN'] as $category){       $sql4 = INSERT INTO book_categories (book_id, categories_id)    Â
Re: [PHP] RE: non-auto increment question
On Mon, Mar 16, 2009 at 03:46:29PM -0500, PJ wrote: snip $book_publisher = array(); $SQL = SELECT id, bookID, publisher_id FROM book_publisher; if ( ( $results = mysql_query($SQL, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $book_publisher[$row['bookID']][] = $row['publisher_id']; Could you explain this, please - this is further from me than Arcturus: $book_publisher[$row['bookID']][] = $row['publisher_id']; What is purpose and meaning of all the [] brackets? Especially the empty ones? I following your example, I have been able to finally get all the arrays in place. Only now, I am looking into how to retrieve the parts of the array... PJ, you *really* need to read up on the *basics* of the PHP language. Please buy a good book on the subject, or search php.net for arrays and read and understand what is there. To summarize, $book_publisher is an array, as you can tell by the square brackets after it. Its first index is book IDs. So in the expression $book_publisher[$row['bookID']], $row['bookID'] is the index for one of the members of the array. It could be 5 instead, as in: $book_publisher[5]. Likewise, $row is an array. When you see an expression like $array['something'] or $array[1], the value being represented is actually the value at that index of the array. If you have an array of letters called $alphabet, it might look something like this: indexes: 'a', 'b', 'c', 'd', 'e' contents: 'alfa', 'bravo', 'charlie', 'delta', 'echo' So if you had an expression like: $alphabet['d'], it would actually represent the value 'delta'. Read the above until that's clear. In the case of $row['bookID'], it's just a value at the 'bookID' index of the $row array. It could be '1234', '55-345' or whatever. So $book_publisher[$row['bookID']] is the array member at the $row['bookID'] index of the array. Lastly, since an array member can also be another array, you can have multidimensional arrays. In your case, it's: $book_publisher[$row['bookID']][] Notice the last set of open and close backets on the end. That tells you that $book_publisher[$row['bookID']] is an array member which is actually another array. But empty open/close brackets have a special meaning. You will only find them (meaningfully) on the left hand side of an equation. They mean that you're adding whatever is on the right side of the equation to the last position in the array. So $book_publisher[$row['bookID']][] = $row['publisher_id'] Says that the $row['bookID'] member of $book_publisher is an array, and that you're adding a member on the end whose value is $row['publisher_id']. Depending on your mental abilities, you may have to draw the arrays on a sheet of paper to make it make sense. Also remember that arrays can be indexed either numerically or associatively or both. A numerically indexed arrays would have just numbers for the indexes. An associatively indexed array is what you have above, indexed not necessarily by number but possibly by strings or whatever. All this is fundamental to array processing under PHP and used constantly. So learn it and learn it well. If my explanation is insufficient, then look it up and fully understand it before you go on. Otherwise, you'll take one step, ask a bunch of questions, take another step, ask a bunch of questions, and generally make yourself a nuisance. You're essentially asking professional programmers to tutor you for free when you really should take the initiative and read/study the subject yourself. It doesn't matter if you don't really want to *be* a professional programmer at the end of this. You have to approach it as though that's your goal. Let me put this another way. Assume you're a professional chef. You get paid lots of money to cook food for people. You've had years of education and practice and get paid accordingly. Suddenly one day, management sends you some wet-behind-the-ears kid who's flipped burgers at McDonalds, and you're supposed to teach him how to cook like you do. Every time he cuts a shallot or dices an onion, he asks you a bunch of silly questions he should know if he had gone to culinary school like you did. You're busy. And the questions are things like What's the difference between a teaspoon and a tablespoon?. How come I have to dice onions? Can't I just throw them in the pot? And you know that the only reason the kid is there is so he can learn to cook some stuff to impress his girlfriend, who's gonna dump him in three months anyway. Think about it. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Paul M Foster wrote: On Mon, Mar 16, 2009 at 03:46:29PM -0500, PJ wrote: snip $book_publisher = array(); $SQL = SELECT id, bookID, publisher_id FROM book_publisher; if ( ( $results = mysql_query($SQL, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $book_publisher[$row['bookID']][] = $row['publisher_id']; Could you explain this, please - this is further from me than Arcturus: $book_publisher[$row['bookID']][] = $row['publisher_id']; What is purpose and meaning of all the [] brackets? Especially the empty ones? I following your example, I have been able to finally get all the arrays in place. Only now, I am looking into how to retrieve the parts of the array... PJ, you *really* need to read up on the *basics* of the PHP language. Please buy a good book on the subject, or search php.net for arrays and read and understand what is there. To summarize, $book_publisher is an array, as you can tell by the square brackets after it. Its first index is book IDs. So in the expression $book_publisher[$row['bookID']], $row['bookID'] is the index for one of the members of the array. It could be 5 instead, as in: $book_publisher[5]. Likewise, $row is an array. When you see an expression like $array['something'] or $array[1], the value being represented is actually the value at that index of the array. If you have an array of letters called $alphabet, it might look something like this: indexes: 'a', 'b', 'c', 'd', 'e' contents: 'alfa', 'bravo', 'charlie', 'delta', 'echo' So if you had an expression like: $alphabet['d'], it would actually represent the value 'delta'. Read the above until that's clear. In the case of $row['bookID'], it's just a value at the 'bookID' index of the $row array. It could be '1234', '55-345' or whatever. So $book_publisher[$row['bookID']] is the array member at the $row['bookID'] index of the array. Lastly, since an array member can also be another array, you can have multidimensional arrays. In your case, it's: $book_publisher[$row['bookID']][] Notice the last set of open and close backets on the end. That tells you that $book_publisher[$row['bookID']] is an array member which is actually another array. But empty open/close brackets have a special meaning. You will only find them (meaningfully) on the left hand side of an equation. They mean that you're adding whatever is on the right side of the equation to the last position in the array. So $book_publisher[$row['bookID']][] = $row['publisher_id'] Says that the $row['bookID'] member of $book_publisher is an array, and that you're adding a member on the end whose value is $row['publisher_id']. Depending on your mental abilities, you may have to draw the arrays on a sheet of paper to make it make sense. Also remember that arrays can be indexed either numerically or associatively or both. A numerically indexed arrays would have just numbers for the indexes. An associatively indexed array is what you have above, indexed not necessarily by number but possibly by strings or whatever. All this is fundamental to array processing under PHP and used constantly. So learn it and learn it well. If my explanation is insufficient, then look it up and fully understand it before you go on. Otherwise, you'll take one step, ask a bunch of questions, take another step, ask a bunch of questions, and generally make yourself a nuisance. You're essentially asking professional programmers to tutor you for free when you really should take the initiative and read/study the subject yourself. It doesn't matter if you don't really want to *be* a professional programmer at the end of this. You have to approach it as though that's your goal. Let me put this another way. Assume you're a professional chef. You get paid lots of money to cook food for people. You've had years of education and practice and get paid accordingly. Suddenly one day, management sends you some wet-behind-the-ears kid who's flipped burgers at McDonalds, and you're supposed to teach him how to cook like you do. Every time he cuts a shallot or dices an onion, he asks you a bunch of silly questions he should know if he had gone to culinary school like you did. You're busy. And the questions are things like What's the difference between a teaspoon and a tablespoon?. How come I have to dice onions? Can't I just throw them in the pot? And you know that the only reason the kid is there is so he can learn to cook some stuff to impress his girlfriend, who's gonna dump him in three months anyway. Think about it. Paul Thanks Paul for the explanation. It really does help and iis much appreciated. I must respond to your chastisement of me, because i believe you may not be aware of the fact that most books on programming really suck. I have had my taste of enough of them to not bother. The kinds of things that I am trying to do just cannot be learned from a book or from most of
Re: [PHP] RE: non-auto increment question
On Mon, Mar 16, 2009 at 06:05:33PM -0500, PJ wrote: Paul M Foster wrote: snip Thanks Paul for the explanation. It really does help and iis much appreciated. I must respond to your chastisement of me, because i believe you may not be aware of the fact that most books on programming really suck. I have had my taste of enough of them to not bother. The kinds of things that I am trying to do just cannot be learned from a book or from most of those tutorials one finds on the Internet. How do you think *I* learned this? Back in 1974, when I hooked up my high school teletype via an acoustic modem to the school district mainframe and logged in for the first time, I had one choice for programming languages: Mainframe (Dartmouth) BASIC. So I bought a book on the subject, still in print today, called Basic Basic. Thus I learned Pascal (Turbo Pascal). Thus I learned C (Kernighan and Richie). Thus I learned Linux (man pages). Thus I learned Perl, Python and PHP. *All* from books. I didn't have anyone to ask. Even when I got a job as FoxPro programmer, I had only FoxPro books to tell me what to do. If I'd pestered the other coders by asking them questions about how to do things all the time, they would have fired me on the spot. I benefitted there from having programmed in other languages before. FoxPro was then just another language. The only disadvantage to my approach is that I sometimes wonder if, having learned all this stuff in a virtual vacuum, everyone else knows a better way than me. But my experience has been that that is infrequently the case. Yes, programming books *can* suck. And most assume you know more than you do. I benefit because I programmed in C before I programmed in PHP, which is virtually C for the web. And my C benefitted from knowing Pascal before that and BASIC before that. Multi-dimensional arrays are part and parcel of Pascal and C. So encountering them in PHP was no big deal. Syntax varies from language to language (in Python, you don't do $arr[] = 'blah'; you do arr.append('blah')), but the concept is the same. Once you know the concept well, the syntax is relatively simple to learn, unless you're trying to learn Scheme or something (;-}). They are either moronically basic of sadly flawed or outdated. It's something like the do-it-yourself-wikis... any dolt can put of whatever they think. That's the downside of the net, of course. The upside is that there are great people, like yourself and a number of other who have been very helpful and who are willing to share their knowledge. Anyway, I find very little in the tutorials on the web that goes beyond total basics. You're not looking hard enough. I've found help with a great many esoteric (edge case) problems by searching on the net. You can't just decide Well, all the books suck and the net sucks, so I'll just ask someone who knows. That's why they write books. So people don't have to use up all the time of people who know, by asking them masses of questions. The people who know are busy producing products. Professors and teachers get paid to teach people things, not productive professionals in a field. You're lucky the professionals are willing to spare the (free) time to explain what already plainly exists in books. Learning PHP5 is a crappy little O'Reilly book for anyone who's programmed in another language and wants to learn PHP. But it's perfectly fine for a non-programmer learning PHP. Don't get me wrong. I have nothing against you personally. And I'm more than willing to share my knowledge with others in general. I allows me to clarify my concepts and gives others an opportunity to dispute what I'm saying. And of course, it helps others get it, when I know it's sometimes hard to figure out a way through. But I've done enough years of tech support and instruction to recognize the symptoms I'm seeing here. I'll let other people hold your hand from now on. And when they get enough of it, they'll chastise you as well. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Paul M Foster wrote: On Mon, Mar 16, 2009 at 06:05:33PM -0500, PJ wrote: Paul M Foster wrote: snip Thanks Paul for the explanation. It really does help and iis much appreciated. I must respond to your chastisement of me, because i believe you may not be aware of the fact that most books on programming really suck. I have had my taste of enough of them to not bother. The kinds of things that I am trying to do just cannot be learned from a book or from most of those tutorials one finds on the Internet. How do you think *I* learned this? Back in 1974, when I hooked up my high school teletype via an acoustic modem to the school district mainframe and logged in for the first time, I had one choice for programming languages: Mainframe (Dartmouth) BASIC. So I bought a book on the subject, still in print today, called Basic Basic. Thus I learned Pascal (Turbo Pascal). Thus I learned C (Kernighan and Richie). Thus I learned Linux (man pages). Thus I learned Perl, Python and PHP. *All* from books. I didn't have anyone to ask. Even when I got a job as FoxPro programmer, I had only FoxPro books to tell me what to do. If I'd pestered the other coders by asking them questions about how to do things all the time, they would have fired me on the spot. I benefitted there from having programmed in other languages before. FoxPro was then just another language. The only disadvantage to my approach is that I sometimes wonder if, having learned all this stuff in a virtual vacuum, everyone else knows a better way than me. But my experience has been that that is infrequently the case. Yes, programming books *can* suck. And most assume you know more than you do. I benefit because I programmed in C before I programmed in PHP, which is virtually C for the web. And my C benefitted from knowing Pascal before that and BASIC before that. Multi-dimensional arrays are part and parcel of Pascal and C. So encountering them in PHP was no big deal. Syntax varies from language to language (in Python, you don't do $arr[] = 'blah'; you do arr.append('blah')), but the concept is the same. Once you know the concept well, the syntax is relatively simple to learn, unless you're trying to learn Scheme or something (;-}). They are either moronically basic of sadly flawed or outdated. It's something like the do-it-yourself-wikis... any dolt can put of whatever they think. That's the downside of the net, of course. The upside is that there are great people, like yourself and a number of other who have been very helpful and who are willing to share their knowledge. Anyway, I find very little in the tutorials on the web that goes beyond total basics. You're not looking hard enough. I've found help with a great many esoteric (edge case) problems by searching on the net. You can't just decide Well, all the books suck and the net sucks, so I'll just ask someone who knows. That's why they write books. So people don't have to use up all the time of people who know, by asking them masses of questions. The people who know are busy producing products. Professors and teachers get paid to teach people things, not productive professionals in a field. You're lucky the professionals are willing to spare the (free) time to explain what already plainly exists in books. Learning PHP5 is a crappy little O'Reilly book for anyone who's programmed in another language and wants to learn PHP. But it's perfectly fine for a non-programmer learning PHP. Don't get me wrong. I have nothing against you personally. And I'm more than willing to share my knowledge with others in general. I allows me to clarify my concepts and gives others an opportunity to dispute what I'm saying. And of course, it helps others get it, when I know it's sometimes hard to figure out a way through. But I've done enough years of tech support and instruction to recognize the symptoms I'm seeing here. I'll let other people hold your hand from now on. And when they get enough of it, they'll chastise you as well. Paul I understand what you are sayig and I am quite impressed with your knowledge and experience. Just to justify my position and by the way before I forget I hope that there will be others who will benefit from my frustrations as some of the help I have received has been quite high level and that, obviously, I did not find on the web. As to intensifying my search on the web, well, little by little I stumble onto some stuff that is helpful but the amount of time I spend looking and thinking is beyond normal human endurance. And the rewards from searching are extremely minimal. Where can you find the kind of stuff that you and Jim Lucas have offered is not to be found easily if at all on the web. And don't forget how miserable searching has become on Google. The others, I don't even consider and don't have the time. I'll try harder to understand the array but there is a lot that simply is not clear... I keep analyzing the
Re: [PHP] RE: non-auto increment question
PJ wrote: haliphax wrote: On Fri, Mar 13, 2009 at 1:24 PM, PJ af.gour...@videotron.ca wrote: Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book           ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers )         VALUES           ('$titleIN', '$sub_titleIN', '$descrIN',           '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN');     $result1 = mysql_query($sql1, $db);   $autoid = mysql_insert_id($result1);     $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN');       $result2 = mysql_query($sql2, $db);   $authorID = mysql_insert_id($result2);     $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1');       $result2a = mysql_query($sql2a, $db);     $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN');       $result2b = mysql_query($sql2b, $db);   $author2ID = mysql_insert_id($result2b);     $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2');       $result2c = mysql_query($sql2c, $db);     $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN');       $result3 = mysql_query($sql3, $db);   $publisherID = mysql_insert_id($result3);     $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' );       $result3a = mysql_query($sql3a, $db);     foreach($_POST['categoriesIN'] as $category){       $sql4 = INSERT INTO book_categories (book_id, categories_id)         VALUES
Re: [PHP] RE: non-auto increment question
Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN'); $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1); $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN'); $result2 = mysql_query($sql2, $db); $authorID = mysql_insert_id($result2); $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1'); $result2a = mysql_query($sql2a, $db); $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN'); $result2b = mysql_query($sql2b, $db); $author2ID = mysql_insert_id($result2b); $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2'); $result2c = mysql_query($sql2c, $db); $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN'); $result3 = mysql_query($sql3, $db); $publisherID = mysql_insert_id($result3); $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' ); $result3a = mysql_query($sql3a, $db); foreach($_POST['categoriesIN'] as $category){ $sql4 = INSERT INTO book_categories (book_id, categories_id) VALUES ($autoid, $category); $result4 = mysql_query($sql4,$db); } echo $autoid; // shows: blank
Re: [PHP] RE: non-auto increment question
On Fri, Mar 13, 2009 at 1:24 PM, PJ af.gour...@videotron.ca wrote: Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN'); $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1); $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN'); $result2 = mysql_query($sql2, $db); $authorID = mysql_insert_id($result2); $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1'); $result2a = mysql_query($sql2a, $db); $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN'); $result2b = mysql_query($sql2b, $db); $author2ID = mysql_insert_id($result2b); $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2'); $result2c = mysql_query($sql2c, $db); $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN'); $result3 = mysql_query($sql3, $db); $publisherID = mysql_insert_id($result3); $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' ); $result3a = mysql_query($sql3a, $db); foreach($_POST['categoriesIN'] as $category){ $sql4 = INSERT INTO book_categories (book_id, categories_id) VALUES ($autoid, $category); $result4 =
Re: [PHP] RE: non-auto increment question
haliphax wrote: On Fri, Mar 13, 2009 at 1:24 PM, PJ af.gour...@videotron.ca wrote: Jim Lucas wrote: PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book           ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers )         VALUES           ('$titleIN', '$sub_titleIN', '$descrIN',           '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN');     $result1 = mysql_query($sql1, $db);   $autoid = mysql_insert_id($result1);     $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN');       $result2 = mysql_query($sql2, $db);   $authorID = mysql_insert_id($result2);     $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1');       $result2a = mysql_query($sql2a, $db);     $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN');       $result2b = mysql_query($sql2b, $db);   $author2ID = mysql_insert_id($result2b);     $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2');       $result2c = mysql_query($sql2c, $db);     $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN');       $result3 = mysql_query($sql3, $db);   $publisherID = mysql_insert_id($result3);     $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' );       $result3a = mysql_query($sql3a, $db);     foreach($_POST['categoriesIN'] as $category){      Â
Re: [PHP] RE: non-auto increment question
PJ wrote: am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN'); $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1); $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN'); $result2 = mysql_query($sql2, $db); $authorID = mysql_insert_id($result2); $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1'); $result2a = mysql_query($sql2a, $db); $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN'); $result2b = mysql_query($sql2b, $db); $author2ID = mysql_insert_id($result2b); $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2'); $result2c = mysql_query($sql2c, $db); $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN'); $result3 = mysql_query($sql3, $db); $publisherID = mysql_insert_id($result3); $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' ); $result3a = mysql_query($sql3a, $db); foreach($_POST['categoriesIN'] as $category){ $sql4 = INSERT INTO book_categories (book_id, categories_id) VALUES ($autoid, $category); $result4 = mysql_query($sql4,$db); } echo $autoid; // shows: blank echo $authorID; // shows: blank echo $author2ID; // shows: blank echo $publisherID; // shows: blank
Re: [PHP] RE: non-auto increment question
Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN'); $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1); $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN'); $result2 = mysql_query($sql2, $db); $authorID = mysql_insert_id($result2); $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1'); $result2a = mysql_query($sql2a, $db); $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN'); $result2b = mysql_query($sql2b, $db); $author2ID = mysql_insert_id($result2b); $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2'); $result2c = mysql_query($sql2c, $db); $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN'); $result3 = mysql_query($sql3, $db); $publisherID = mysql_insert_id($result3); $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' ); $result3a = mysql_query($sql3a, $db); foreach($_POST['categoriesIN'] as $category){ $sql4 = INSERT INTO book_categories (book_id, categories_id) VALUES ($autoid, $category); $result4 = mysql_query($sql4,$db); } echo $autoid; // shows: blank echo $authorID; // shows: blank echo $author2ID; // shows: blank echo $publisherID; // shows: blank echo brautoid =
Re: [PHP] RE: non-auto increment question
am resending as this was erroneously ccd to mysql. sorry! Ashley Sheridan wrote: On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk For some reason or other $autoId = mysql_insert_id($result); just does not work for me... Yet some of the data is inserted correctly... I did find that it does not work on tables that are empty... so you can't start with an empty table. I entered data it still did not work. I tried on another duplicate database... doesn't work. I have checked double checked the database, I have added checks to see what is returned and the returns are 0 or null - as I get different responses for slightly different functions. sessions is on mysql is 5.1.28 php5 here's what is parsed: else { $sql1 = INSERT INTO book ( title, sub_title, descr, comment, bk_cover, copyright, ISBN, language, sellers ) VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN', '$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN', '$sellersIN'); $result1 = mysql_query($sql1, $db); $autoid = mysql_insert_id($result1); $sql2 = INSERT INTO author (first_name, last_name) VALUES ('$first_nameIN', '$last_nameIN'); $result2 = mysql_query($sql2, $db); $authorID = mysql_insert_id($result2); $sql2a = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$authorID', '$autoid', '1'); $result2a = mysql_query($sql2a, $db); $sql2b = INSERT INTO author (first_name, last_name) VALUES ('$first_name2IN', '$last_name2IN'); $result2b = mysql_query($sql2b, $db); $author2ID = mysql_insert_id($result2b); $sql2c = INSERT INTO book_author (authID, bookID, ordinal) VALUES ( '$author2ID', '$autoid', '2'); $result2c = mysql_query($sql2c, $db); $sql3 = INSERT INTO publishers (publisher) VALUES ('$publisherIN'); $result3 = mysql_query($sql3, $db); $publisherID = mysql_insert_id($result3); $sql3a = INSERT INTO book_publisher (bookID, publishers_id) VALUES ( '$autoid', '$publisherID' ); $result3a = mysql_query($sql3a, $db); foreach($_POST['categoriesIN'] as $category){ $sql4 = INSERT INTO book_categories (book_id, categories_id) VALUES ($autoid, $category); $result4 = mysql_query($sql4,$db); } echo $autoid; // shows: blank echo $authorID; // shows: blank echo $author2ID; // shows: blank echo
Re: [PHP] RE: non-auto increment question
At 10:02 PM + 2/26/09, Ashley Sheridan wrote: Only in America! Ash Not true -- I never thought otherwise. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
On Thu, 2009-02-26 at 11:27 -0500, PJ wrote: Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto-insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
-Original Message- From: PJ [mailto:af.gour...@videotron.ca] Sent: Thursday, February 26, 2009 11:27 AM To: Jerry Schwartz Cc: a...@ashleysheridan.co.uk; 'Gary W. Smith'; 'MySql'; php- gene...@lists.php.net Subject: Re: [PHP] RE: non-auto increment question Jerry Schwartz wrote: Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Thanks, Jerry, You hit the nail on the head.:) [JS] I'm glad to hear it. To refine my problem (and reduce my ignorance),here's what is happening on the form page: There is a series of INSERTs. The first inserts all the columns of book table except for the id, which I do not specify as it if auto- insert. In subsequent tables I have to reference the book.id (for transitional tables like book_author(refers authors to book) etc. [JS] Okay. If I understand it correctly, I must retrieve (SELECT LAST_INSERT_ID()) after the first INSERT and before the following insert; and save the id as a string ($id)...e.g. $sql = SELECT LAST_INSERT_ID() AS $id [JS] You are confusing database column names with PHP variable names. You don't need an alias at all, unless you feel like it for reasons of convenience or style. Assume that $title is your book title, and that the first column is an auto-increment field. The first two queries should look like $query_insert = INSERT INTO book VALUES (NULL, '$title', ...); and $query_select_id = SELECT LAST_INSERT_ID(); Of course, you need to actually execute the two queries. The first one doesn't return anything (check for errors, of course). The second one retrieves the ID of the record you just inserted. Now retrieve the value returned by the SELECT statement and put it into a variable. You'll use something like $row_selected = mysql_query($query_select_id) or die($query_select_id failed); $last_id = mysql_fetch_array($row_selected) or die(Unable to fetch last inserted ID); and you have what you want. You can now use $last_id anywhere you want, until your script ends. This is all very simplified, but I think you can get my drift. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com I need clarification on the AS $id - should this be simply id(does this have to be turned into a value into $id or does $id contain the value? And how do I retrieve it to use the returned value for the next $sql = INSERT ... - in other words, is the id or $id available for the next directive or do I have to do something like $id = id? I'm trying to figure this out with some trials but my insert does not work from a php file - but it works from command-line... that's another post. -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk [JS] Ashley is absolutely right, I'd forgotten about the mysql_insert_id shorthand. (I'm a one-man band, and for the last week or two I've been immersed in VB for Access forms.) Not only is she right, but her way is better. Presumably a language's internal code is maintained as the specific database changes. You can make yourself more independent of the specific database by using the PDO abstraction, although I would save that for a rainy weekend. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
On Thu, 2009-02-26 at 13:44 -0500, Jerry Schwartz wrote: Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk [JS] Ashley is absolutely right, I'd forgotten about the mysql_insert_id shorthand. (I'm a one-man band, and for the last week or two I've been immersed in VB for Access forms.) Not only is she right, but her way is better. Presumably a language's internal code is maintained as the specific database changes. You can make yourself more independent of the specific database by using the PDO abstraction, although I would save that for a rainy weekend. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com I just checked, and yep, I'm definitely still a he ;) Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Ashley Sheridan wrote: On Thu, 2009-02-26 at 13:44 -0500, Jerry Schwartz wrote: Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk [JS] Ashley is absolutely right, I'd forgotten about the mysql_insert_id shorthand. (I'm a one-man band, and for the last week or two I've been immersed in VB for Access forms.) Not only is she right, but her way is better. Presumably a language's internal code is maintained as the specific database changes. You can make yourself more independent of the specific database by using the PDO abstraction, although I would save that for a rainy weekend. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com I just checked, and yep, I'm definitely still a he ;) I never thought otherwise... but then I was wondering... there are too many actresses with the same name... ;-) -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
Sorry, I should know better. -Original Message- From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk] Sent: Thursday, February 26, 2009 1:51 PM To: Jerry Schwartz Cc: 'PJ'; 'Gary W. Smith'; 'MySql'; php-general@lists.php.net Subject: RE: [PHP] RE: non-auto increment question On Thu, 2009-02-26 at 13:44 -0500, Jerry Schwartz wrote: Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk [JS] Ashley is absolutely right, I'd forgotten about the mysql_insert_id shorthand. (I'm a one-man band, and for the last week or two I've been immersed in VB for Access forms.) Not only is she right, but her way is better. Presumably a language's internal code is maintained as the specific database changes. You can make yourself more independent of the specific database by using the PDO abstraction, although I would save that for a rainy weekend. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com I just checked, and yep, I'm definitely still a he ;) Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
On Thu, 2009-02-26 at 14:04 -0500, PJ wrote: Ashley Sheridan wrote: On Thu, 2009-02-26 at 13:44 -0500, Jerry Schwartz wrote: Here's how I mostly do it (albeit simplified): $query = INSERT INTO `sometable`(`title`,`content`) VALUES('$title','$content'); $result = mysql_query($query); $autoId = mysql_insert_id($result); $query = INSERT INTO `another_table`(`link_id`,`value`) VALUES($autoId,'$value'); $result = mysql_query($query); No need to call another query to retrieve the last inserted id, as it is tied to the last query executed within this session. Ash www.ashleysheridan.co.uk [JS] Ashley is absolutely right, I'd forgotten about the mysql_insert_id shorthand. (I'm a one-man band, and for the last week or two I've been immersed in VB for Access forms.) Not only is she right, but her way is better. Presumably a language's internal code is maintained as the specific database changes. You can make yourself more independent of the specific database by using the PDO abstraction, although I would save that for a rainy weekend. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com I just checked, and yep, I'm definitely still a he ;) I never thought otherwise... but then I was wondering... there are too many actresses with the same name... ;-) -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com Only in America! Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS -- The statement is confusing at best. For the casual user auto_increment is the way to do. I say for the casual user. That is typical me and you. Basically if you do an insert a unique value is inserted at the time of the insert. As mentioned, there are ways to get this value back in the return. Now why I say it's for the casual user is because if you are using triggers then you can do things prior to this value being used and then the statement above is correct. But you are not going to be using triggers... So, put an auto_increment on the key field and find one of the 2^16 samples of how this works with PHP. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
On Wed, 2009-02-25 at 14:10 -0800, Gary W. Smith wrote: Not sure that this is the problem BUT you should probably qualify the name of the variable such that SELECT MAX(id) AS id FROM book. But you don't want max(id) as id but rather max(id) + 1 as id. With that you can then just return the final value. Also, if you don't want to alias the value (or whatever it's called) you should use $row[0] to get it by ordinal posistion. As for now wanting to use autoincrement, you can run into a race condition where two people are inserting at the same time, thus having the same generated id. Hope that helps. From: PJ [mailto:af.gour...@videotron.ca] Sent: Wed 2/25/2009 2:01 PM To: MySql; php-general@lists.php.net Subject: non-auto increment question I want to insert a new table entry 1 number higher than the highest in the field (id). I cannot use auto-increment. And I want to show the value of the field to be added in an input field on the web page: if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id) FROM book; $result = mysql_query($sql, $db); $bookCount = mysql_num_rows($result); for ($i=0; $i $bookCount; $i++) { $row = mysql_fetch_array($result); $idIN= $row[id]+1; } $idIN= $_POST[idIN]; $titleIN= $_POST[titleIN]; ...snip... td colspan=2 ? echo input type='text' name='titleIN' value='$idIN' disabled size='2'; ? /td What am I doing wrong? (The query works and returns the right nr. but what do I have to do to add 1 to that number and then display it in the on page and post it to the table? -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.ptahhotep.com/ http://www.chiccantine.com http://www.chiccantine.com/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=g...@primeexalia.com Yeah, this sort of situation is really what auto increment is for. If you get two people visiting the page with this code on at the same time then you'll screw up your database. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
On Wed, 2009-02-25 at 19:03 -0500, PJ wrote: Ashley Sheridan wrote: On Wed, 2009-02-25 at 14:10 -0800, Gary W. Smith wrote: Not sure that this is the problem BUT you should probably qualify the name of the variable such that SELECT MAX(id) AS id FROM book. But you don't want max(id) as id but rather max(id) + 1 as id. With that you can then just return the final value. Also, if you don't want to alias the value (or whatever it's called) you should use $row[0] to get it by ordinal posistion. As for now wanting to use autoincrement, you can run into a race condition where two people are inserting at the same time, thus having the same generated id. Hope that helps. From: PJ [mailto:af.gour...@videotron.ca] Sent: Wed 2/25/2009 2:01 PM To: MySql; php-general@lists.php.net Subject: non-auto increment question I want to insert a new table entry 1 number higher than the highest in the field (id). I cannot use auto-increment. And I want to show the value of the field to be added in an input field on the web page: if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id) FROM book; $result = mysql_query($sql, $db); $bookCount = mysql_num_rows($result); for ($i=0; $i $bookCount; $i++) { $row = mysql_fetch_array($result); $idIN = $row[id]+1; Actually, I am wondering how to get rid of some of the code here as it seems a little bloated How do I get rid of the row counting - since there can never be more than one row returned with this query. } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ...snip... td colspan=2 ? echo input type='text' name='titleIN' value='$idIN' disabled size='2'; ? /td What am I doing wrong? (The query works and returns the right nr. but what do I have to do to add 1 to that number and then display it in the on page and post it to the table? -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.ptahhotep.com/ http://www.chiccantine.com http://www.chiccantine.com/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=g...@primeexalia.com Yeah, this sort of situation is really what auto increment is for. If you get two people visiting the page with this code on at the same time then you'll screw up your database. Ash www.ashleysheridan.co.uk Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS The auto increment value is automatically inserted by MySQL, just don't specify what to put into that field when executing the sql statement in PHP. You can have PHP return the value that was just inserted using the auto_insert_id() function, which allows you to use it for other things, like inserting into other tables for a relational database structure. Also, depending on how the page is called, you could end up with one user inserting the same value into the database. if the page is called via get (i.e. a link on a page rather than as the result of a form) then some browsers will actually (in an attempt to make things faster) call the page more than once. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Ashley Sheridan wrote: On Wed, 2009-02-25 at 14:10 -0800, Gary W. Smith wrote: Not sure that this is the problem BUT you should probably qualify the name of the variable such that SELECT MAX(id) AS id FROM book. But you don't want max(id) as id but rather max(id) + 1 as id. With that you can then just return the final value. Also, if you don't want to alias the value (or whatever it's called) you should use $row[0] to get it by ordinal posistion. As for now wanting to use autoincrement, you can run into a race condition where two people are inserting at the same time, thus having the same generated id. Hope that helps. From: PJ [mailto:af.gour...@videotron.ca] Sent: Wed 2/25/2009 2:01 PM To: MySql; php-general@lists.php.net Subject: non-auto increment question I want to insert a new table entry 1 number higher than the highest in the field (id). I cannot use auto-increment. And I want to show the value of the field to be added in an input field on the web page: if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id) FROM book; $result = mysql_query($sql, $db); $bookCount = mysql_num_rows($result); for ($i=0; $i $bookCount; $i++) { $row = mysql_fetch_array($result); $idIN = $row[id]+1; Actually, I am wondering how to get rid of some of the code here as it seems a little bloated How do I get rid of the row counting - since there can never be more than one row returned with this query. } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ...snip... td colspan=2 ? echo input type='text' name='titleIN' value='$idIN' disabled size='2'; ? /td What am I doing wrong? (The query works and returns the right nr. but what do I have to do to add 1 to that number and then display it in the on page and post it to the table? -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.ptahhotep.com/ http://www.chiccantine.com http://www.chiccantine.com/ -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=g...@primeexalia.com Yeah, this sort of situation is really what auto increment is for. If you get two people visiting the page with this code on at the same time then you'll screw up your database. Ash www.ashleysheridan.co.uk Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS [JS] Are you looking for something like LAST_INSERT_ID()? If you INSERT a record that has an auto-increment field, you can retrieve the value that got inserted with SELECT LAST_INSERT_ID(). It is connection-specific, so you'll always have your own value. You can then save it to reuse, either as a session variable or (more easily) as a hidden field on your form. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=jschwa...@the- infoshop.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RE: non-auto increment question
Being rather new to all this, I understood from the MySql manual that the auto_increment is to b e used immediately after an insertion not intermittently. My application is for administrators (the site owner designates) to update the database from and administration directory, accessed by user/password login... so there's really very little possibility of 2 people accessing at the same time. [JS] Being rather old to all this, I can tell you that if something is even remotely possible it will happen just before your performance review. Never depend upon this. Regards, Jerry Schwartz The Infoshop by Global Information Incorporated 195 Farmington Ave. Farmington, CT 06032 860.674.8796 / FAX: 860.674.8341 www.the-infoshop.com www.giiexpress.com www.etudes-marche.com By using MAX + 1 I keep the id number in the $idIn and can reuse it in other INSERTS -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=jschwa...@the- infoshop.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
PJ wrote: From: PJ [mailto:af.gour...@videotron.ca] Sent: Wed 2/25/2009 2:01 PM To: MySql; php-general@lists.php.net Subject: non-auto increment question I want to insert a new table entry 1 number higher than the highest in the field (id). I cannot use auto-increment. And I want to show the value of the field to be added in an input field on the web page: if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id) FROM book; $result = mysql_query($sql, $db); $bookCount = mysql_num_rows($result); for ($i=0; $i $bookCount; $i++) { $row = mysql_fetch_array($result); $idIN = $row[id]+1; Actually, I am wondering how to get rid of some of the code here as it seems a little bloated How do I get rid of the row counting - since there can never be more than one row returned with this query. Ok, so, you want to know how to do this your way with a little less code? Give this a try: ?php $id = null; if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id)+1 AS id FROM book LIMIT 1; if ( ( $result = mysql_query($sql, $db) ) !== false ) { while ( $row = mysql_fetch_row($result) ) { list($id) = $row; } } } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ? td colspan=2 input type='text' name='id' value='?php echo $id; ?' disabled=disabled size='2' / /td } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ...snip... td colspan=2 ? echo input type='text' name='titleIN' value='$idIN' disabled size='2'; ? Ok, so, I am confused. You are asking about and $id variable, yet you show us code that attempts to insert the $idIN variable into a hidden field for the $titleIN variable. Did you cut/paste the wrong segment of code or is this what you are actually trying to use? if it is the latter, then that explains why your above example is not working like you would expect... Anyways, you will note that in the above example I corrected the variable/name mismatch. If I have it wrong sorry. /td What am I doing wrong? (The query works and returns the right nr. but what do I have to do to add 1 to that number and then display it in the on page and post it to the table? -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RE: non-auto increment question
Jim Lucas wrote: PJ wrote: From: PJ [mailto:af.gour...@videotron.ca] Sent: Wed 2/25/2009 2:01 PM To: MySql; php-general@lists.php.net Subject: non-auto increment question I want to insert a new table entry 1 number higher than the highest in the field (id). I cannot use auto-increment. And I want to show the value of the field to be added in an input field on the web page: if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id) FROM book; $result = mysql_query($sql, $db); $bookCount = mysql_num_rows($result); for ($i=0; $i $bookCount; $i++) { $row = mysql_fetch_array($result); $idIN = $row[id]+1; Actually, I am wondering how to get rid of some of the code here as it seems a little bloated How do I get rid of the row counting - since there can never be more than one row returned with this query. Ok, so, you want to know how to do this your way with a little less code? Give this a try: ?php $id = null; if (isset($_REQUEST[AddNewBooksRequest])) { $SQL = SELECT MAX(id)+1 AS id FROM book LIMIT 1; if ( ( $result = mysql_query($sql, $db) ) !== false ) { Note: the above will not work either. You need to watch the case on your variable names. You define $SQL = '' but then use $sql... That won't work, you need to have them the same case! while ( $row = mysql_fetch_row($result) ) { list($id) = $row; } } } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ? td colspan=2 inputtype='text' name='id' value='?php echo $id; ?' disabled=disabled size='2' / /td } $idIN = $_POST[idIN]; $titleIN = $_POST[titleIN]; ...snip... td colspan=2 ? echo input type='text' name='titleIN' value='$idIN' disabled size='2'; ? Ok, so, I am confused. You are asking about and $id variable, yet you show us code that attempts to insert the $idIN variable into a hidden field for the $titleIN variable. Did you cut/paste the wrong segment of code or is this what you are actually trying to use? if it is the latter, then that explains why your above example is not working like you would expect... Anyways, you will note that in the above example I corrected the variable/name mismatch. If I have it wrong sorry. /td What am I doing wrong? (The query works and returns the right nr. but what do I have to do to add 1 to that number and then display it in the on page and post it to the table? -- -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php