Why can't you use one query? UPDATE Book2 SET stock = stock - $quantity WHERE stock >= $quantity AND booktitle = '$booktitle'
---John Holmes... > -----Original Message----- > From: Matthew Ward [mailto:[EMAIL PROTECTED]] > Sent: Monday, May 13, 2002 11:52 AM > To: [EMAIL PROTECTED] > Subject: [PHP] Re: PHP and mySQL > > I presume that "quantity" is the number of books that the person has > ordered, and therefore it needs to be a variable (ie with a $ infront of > it) > and its also best to do the calculation outside of the SQL statement just > to > be sure it works, eg: > > if ($submit){ > if(isset($booktitle) && isset($quantity)){ > $retrievestock = mysql_query("SELECT stock FROM Book2 WHERE > booktitle > = '$booktitle'"); > while($getstock = mysql_fetch_array($retrievestock)) { > $stockamount = $getstock[stock]; > } > > $newamount = $stockamount - $quantity; > > $sql = mysql_query("UPDATE Book2 SET stock = '$newamount' WHERE > booktitle= '$booktitle'"); > if(! $sql) { print("Could not update stock amount."); > } elseif(! $submit) { > print("Your order has not been placed.<p>"); > } > > > "City Colleges Of Chicago - Mannheim" <[EMAIL PROTECTED]> wrote in > message > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > I am a student working on a practicum problem. I have a mySQL database > that > > contains the books, their title, and how many are in stock. When a > person > > orders one of the books, I want the stock to be adjusted by how many, > > quantity, that they chose when the submit button is clicked. The > scripts > > are written in PHP. > > Here is part of my code: > > <P>The book you are ordering: > > > > <? > > echo " $booktitle "; > > ?> > > > > <P>Additional Message:<br> > > <textarea name="message" cols=30 rows=3></textarea> > > </p> > > <INPUT type="submit" value="Send your order."> > > <input type=hidden name="booktitle" value="<? print $booktitle; ?>"> > > </FORM> > > > > <?php > > $user = "adminer"; > > $pass = "hoosiers"; > > $db = "Book Store1"; > > $local = "jolinux"; > > $link = mysql_connect( "$local", $user, $pass ); > > if (! $link ) > > die ( "Couldn't open the database" ); > > mysql_select_db( $db, $link ) > > or die ( "Couldn't open the $db: ".mysql_error() ); > > > > if ($submit){ > > if( $booktitle, 'quantity' ){ > > $sql = "UPDATE Book2 SET stock ='$stock-quantity' WHERE > booktitle=$booktitle > > AND quantity=quantity"; > > } > > // $result = mysql_query($mysql); > > }else if(!$submit){ > > echo "Your order has not been placed.<p>"; > > } > > ?> > > </BODY> > > </HTML> > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php