Author Topic: Adding a defined quantity  (Read 501 times)

0 Members and 1 Guest are viewing this topic.

Offline phpstuckTopic starter

  • Enthusiast
  • Posts: 56
    • View Profile
Adding a defined quantity
« on: February 06, 2010, 08:07:16 AM »
I am building an inventory tracking program. I have configured a barcode scanner to enter info into the inventory data base, this all works great. The problem I am having is trying to update a field in the database named quantity. On the form for scanning there are two fields... First is quantity, second is UPC... We manually enter 10 for the 10 new cans of corn we are adding with the same UPC number.... I need the database to find the current number in the database and then add the 10 I just scanned. I'm not able to find much on this I have googled and come up with nothing.

Code: [Select]
<?PHP

include_once 'test.html';
include_once 'db.php';

$quan = $_POST['quan'];
$upc = $_POST["upc"];

echo $upc;
echo "<BR>";


$contlist=mysql_query(
        "SELECT * FROM inven WHERE upc='$_POST[upc]'");

while ($all = mysql_fetch_array($contlist)) {
$quan1 = $all['quant'];
$upc1 = $all['upc'];
$brand = $_SESSION['brand'];
$descrip = $all['descrip'];
$size = $all['size'];
$flavor = $all['flavor'];
$cat = $all['cat'];
}




//check that upc does not already exist

$sql_user_check = mysql_query("SELECT upc FROM inven
                         WHERE upc='$_POST[upc]'");

$user_check = mysql_num_rows($sql_user_check);

if(($user_check > 0)){
  echo "<center><b><font face='tahoma' color='black'>Updated ".$descrip." </b><br />";

//RIGHT HERE IS WHERE I AM STRUGGLING

$sql = mysql_query("UPDATE  inven SET quant='(quant)+($_POST[quan]')
WHERE upc='$_POST[upc]'");
                   

  if(!$sql){
    echo 'A database error occurred while creating your account.';
    }
 

   }else{

Offline alexjb

  • Irregular
  • Posts: 20
    • View Profile
Re: Adding a defined quantity
« Reply #1 on: February 08, 2010, 04:40:18 AM »
Maybe you can clarify some things for me. Does this query only return one row?

$contlist=mysql_query(
        
"SELECT * FROM inven WHERE upc='$_POST[upc]'");


If so, then the $quan1 variable contains the current quantity, and you just want to increase this by 10, right? In which case, your update query would simply be:

$sql mysql_query("UPDATE  inven SET quant='" . ($quan1 intval($_POST[quan])) . "')

Something along those lines.