Help please with PHP Database update.

Discussion in 'PHP' started by Jamie T, Oct 11, 2008.

  1. #1
    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.
     
    Jamie T, Oct 11, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    send me the script...
     
    happpy, Oct 11, 2008 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    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!
     
    EricBruggema, Oct 11, 2008 IP
  4. Jamie T

    Jamie T Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    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));
    }
    ?>
     
    Jamie T, Oct 11, 2008 IP
  5. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #5
    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, Oct 11, 2008 IP
  6. Jamie T

    Jamie T Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    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.
     
    Jamie T, Oct 12, 2008 IP
  7. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #7
    no problem man enjoy it
     
    happpy, Oct 12, 2008 IP