Hello. I cannot crack this probably simple problem for the life of me. I'm using a PHP page with MySQL to return product details from my database. It tells me how many units of this product I have. I want enter in a form how many units I'm taking from the inventory. In other words, my results page says I have 100 units (which it does). I type '10' into my form and it updates my database to 90 unilts. At the moment the form just replaces the number of units with the number I enter in the form. So it now thinks I have 10 units, not 90. Surely that can't be too difficult but I'm no wizard with PHP so an easy to follow relpy would be very very much appreciated. Thanks. Jamie.
UPDATE product SET units = units - 10 WHERE productID = 10 (for example) And first you need to check if the unit count isn't higher then the number in your database!
Thanks happpy. This is the whole section. 'spr_code' is the items unique 'spare code'. 'curstock' is the number of these units in the database. 'form3 / textfield' is the form to enter how mant have been removed. I appreciate you taking the time to help. <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form3")) { $updateSQL = sprintf("UPDATE spares SET curstock=%s WHERE spr_code=%s", GetSQLValueString($_POST['textfield'], "text"), GetSQLValueString($_POST['spr_code'], "text")); mysql_select_db($database_Buttercup, $Buttercup); $Result1 = mysql_query($updateSQL, $Buttercup) or die(mysql_error()); $updateGoTo = "store.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } ?>
try this, should work: <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form3")) { $updateSQL = sprintf("UPDATE spares SET curstock=curstock-%s WHERE spr_code=%s", GetSQLValueString($_POST['textfield'], "text"), GetSQLValueString($_POST['spr_code'], "text")); mysql_select_db($database_Buttercup, $Buttercup); $Result1 = mysql_query($updateSQL, $Buttercup) or die(mysql_error()); $updateGoTo = "store.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } ?> Code (markup):
happpy my friend that worked beautifully. Just a small alteration but I would've never gotten that. Thankyou Thankyou Thankyou from Australia. Best Wishes, Jamie.