Re: RE: non-auto increment question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
PJ wrote:
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.


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">
    <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?

--





--
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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux