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.gourmet@xxxxxxxxxxxx] > >> Sent: Wed 2/25/2009 2:01 PM > >> To: MySql; php-general@xxxxxxxxxxxxx > >> 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 --- pj@xxxxxxxxxxxxx > >> 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=gary@xxxxxxxxxxxxxxx > >> > >> > >> > > 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