Re: RE: non-auto increment question

From: Date: Thu, 26 Feb 2009 06:59:32 +0000
Subject: Re: RE: non-auto increment question
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-288807@lists.php.net to get a copy of this message
PJ wrote:
From: PJ [mailto:af.gourmet@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? --


« previous php.general (#288807) next »