[Solved]Trying to update field, Please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

[Solved]Trying to update field, Please help

Post by shab620 »

Hye I'm trying to update a quantity field in a seperate table. Basically what i want to do is select the Item_id and the current quantity from that table. I then want to minus the quantity by 1 and the update the orders table.

I've got to a certain point and come to a brick wall

can someone help???
8O

the php is as follows

Code: Select all

<?php
$item_id=$_POST['special'];
$comments=$_POST['comments'];
$quantity=$_POST['quantity'];
$user_name=$_POST['username'];
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="********"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = ;
mysql_select_db($database);


$result = mysql_query ("SELECT Special_quantity, Special_ID FROM specials ORDER BY Special_ID DESC LIMIT 1",$connect);

//$row = mysql_fetch_*($result);
//or die("Invalid query: " . mysql_error());


while ($row = mysql_fetch_row($result))

{
foreach ($row as $attribute);
}
$total = $attribute;
$newtotal = $total - 1;
$query = ("UPDATE specials SET");
//mysql_query($query);


PRINT '<br><font size ="4" colour="blue">';
IF (mysql_query($query))
PRINT "<center>Order Has Been Placed</Center></font><BR><BR>";
PRINT $attribute;

mysql_close($connect);
?>
Havnt got past the update, can somebody give me a hand???

Shab
8O 8O
Last edited by shab620 on Fri Mar 25, 2005 11:18 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

first off, I hope you change your password. the foreach isn't needed, nor the while loop, as you are requesting only 1 record right now.

If you only want to subtract one from the record found by ordering by Special_ID then you only need an update query, no selection.

Code: Select all

UPDATE `specials` SET `Special_ID` = `Special_ID` - 1 ORDER BY `Special_ID` DESC LIMIT 1
I believe..
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Thanks for that feyd

I've tried incorporating that into my main query now but the problem is that it doesnt run the update of quantity,

the previous insert statement works fine and it inserts ibto the order table

I also dont get any error messages when executing

the code is as follws:

Code: Select all

<?php
$item_id=$_POST['Specials'];
$comments=$_POST['comments'];
$quantity=$_POST['quantity'];
$user_name=$_POST['username'];
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="********"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = emp_order;
mysql_select_db($database);

$result = mysql_query ("SELECT item_Description from item where item_id = '$item_id'",$connect);
//$row = mysql_fetch_*($result);
//or die("Invalid query: " . mysql_error());


while ($row = mysql_fetch_row($result))

{
foreach ($row as $attribute);
}
$query = ("INSERT INTO emp_order VALUES ('','$attribute','$quantity','$comments','$user_name','','Uncomplete')");
//mysql_query($query);

$query1 = ("UPDATE specials SET Special_quantity = Special_quantity - 1 ORDER BY Special_ID DESC LIMIT 1");

PRINT '<br><font size ="4" colour="blue">';
IF (mysql_query($query))
PRINT "<center>Order Has Been Placed</Center></font><BR><BR>";
PRINT $attribute;



mysql_close($connect);
?>
Can you see where i'm going wrong???
Thanks for the help so far

shab
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$query1 isn't executed.

You aren't getting any error messages because you haven't asked for error messages to display.. you commented out or removed the "or die(mysql_error())" stuffs.. :?
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Thanks feyd, I've sorted that problem with your help

Shab

:D :D
Post Reply