title: Code required to automatically update database - fix needed
body: <p>I could really do with some help. I've successfully managed to display how many items I have in stock on my product page using PHP code with help from other members which I am most grateful for. </p>
  
  <p>I'm very new to PHP and have been trying to figure this out for days.</p>
  
  <p>I have a database set up with a table called 'items' which I would like to update automatically if possible. I know I can do this manually but I want to prevent over selling stock I have not got.</p>
  
  <p>For instance, when a customer has placed an order I would like for this to happen:</p>
  
  <p>1) Get the quantity of items in stock under 'inventory' and reduce the number by 1.
  2) Have the new quantity in stock replace the old quantity displayed on my product page.</p>
  
  <p>Now, I use Mals-e for my shopping cart and have all orders sent to the database with a remote call. This is working fine so no problems with info going to the database.</p>
  
  <p>This is the code I use to display the quantity:</p>
  
  <pre><code>&lt;?php
  include("config.php");
  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");
  
  mysql_select_db($DBName) or die("Unable to select database $DBName");    
  
  // Get a specific result from the "items" table
  
      $result = mysql_query("SELECT * FROM items
      WHERE Product='Baby Girls Sock Cupcake'");
  
      while($row = mysql_fetch_array($result))
        {
        echo "Available Stock: ";
        echo $row['inventory'] . " " . $row['Inventory'];
        } 
  
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>This is my form code:</p>
  
  <pre><code>&lt;?php
  echo "&lt;form action=\"http://ww6.aitsafe.com/cf/add.cfm\" method=\"post\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"userid\" value=\"12345678\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"id\" value=\"6\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"scode\" value=\"ABCD123\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"nocart\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"return\" value=\"http://www.mysite.co.uk\"&gt;\n";
  echo "&lt;b&gt;Qty:&lt;/b&gt; &lt;input type=\"text\" name=\"qty\" size=\"1\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"product\" value=\"Socks\"&gt;\n";
  echo "&lt;input type=\"hidden\" name=\"price\" value=\"1.85\"&gt;\n";
  echo "&lt;input name=\"submit\" value=\"Add to Cart\" type=\"submit\"&gt;\n";
  echo "&lt;/form&gt;";
  ?&gt;
  </code></pre>
  
  <p>These are other bits of code I think I may need but I don't know what to do with them or if they are really what I need:</p>
  
  <pre><code>&lt;?php
  $ud_id = $_POST['ud_id'];
  $ud_scode = $_POST['ud_scode'];
  $ud_product = $_POST['ud_product'];
  $ud_price = $_POST['ud_price'];
  $ud_inventory = $_POST['ud_inventory'];
  
  
  // query update
  
  $sql = "UPDATE items SET `items` (`ID`, `$ud_Scode`, `$ud_Product`, `$ud_Price`,   `$ud_Inventory` WHERE inventory=`$ud_Inventory`) 
  
  VALUES (\'6\', \'ABC123\', \'Socks\', \'1.85\', \'2\');";
  
  ?&gt;
  </code></pre>
  
  <p>I notice in this line of code:</p>
  
  <pre><code>echo "&lt;form action=\"http://ww6.aitsafe.com/cf/add.cfm\" method=\"post\"&gt;\n";
  </code></pre>
  
  <p>Other people have a link to a php page:</p>
  
  <pre><code>echo "&lt;form action=\"update.php\" method=\"post\"&gt;\n";
  </code></pre>
  
  <p>Does the link have to be to a php page?</p>
  
  <p>I also have this:</p>
  
  <pre><code>&lt;?php
  include("config.php");
  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");    
  mysql_select_db($DBName) or die("Unable to select database $DBName");   
  
  // Get a specific result from the "items" table
  
  $result = mysql_query("SELECT * FROM items
  WHERE inventory='$inventory'") or die(mysql_error());  
  
  while($row = mysql_fetch_array($result))
  
  // check stock
  
  if        ($row !== false)
            {
             echo 'out of stock';
             }
  
  else if  ($row['count']  == 0)
              {
              echo 'out of stock.';
              }
  
  // if there is stock decrease quantity
  
             $sql = "UPDATE items SET inventory = (inventory -1) WHERE id = '6'";
  
  
  $result = mysql_query;
  
  }  
  
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>I know it's not right and I've got it completely wrong. Could someone please point me in the right direction with an example of code that I need, as I think I need more than this to get it to work. </p>
  
  <p>My htm code and php code are on separate pages, I echo the available quantity in the htm page.</p>
  
  <p>Many Thanks.</p>
  
  <p>This is the code that is used to update the database when an order is placed with remote call fro mals-e:</p>
  
  <pre><code>    &lt;?PHP
  include("sbconf/config.php");
  mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database $DBName");
  
  mysql_select_db($DBName) or die("Unable to select database $DBName");
  
  $order_date=time("m j y h:i");
  
  // create varibles
  
  $id = $_POST['id'];
  $username = $_POST['username'];
  $ip = $_POST['ip'];
  $method = $_POST['method'];
  $cart = $_POST['cart'];
  $discount = $_POST['discount'];
  $subtotal = $_POST['subtotal'];
  $shipping = $_POST['shipping'];
  $tax = $_POST['tax'];
  $total = $_POST['total'];
  $shipping_zone = $_POST['shipping_zone'];
  $inv_name = $_POST['inv_name'];
  $inv_company = $_POST['inv_company'];
  $inv_addr1 = $_POST['inv_addr1'];
  $inv_addr2 = $_POST['inv_addr2'];
  $inv_state = $_POST['inv_state'];
  $inv_zip = $_POST['inv_zip'];
  $inv_country = $_POST['inv_country'];
  $del_name = $_POST['del_name'];
  $del_addr1 = $_POST['del_addr1'];
  $del_addr2 = $_POST['del_addr2'];
  $del_state = $_POST['del_state'];
  $del_zip = $_POST['del_zip'];
  $del_country = $_POST['del_country'];
  $tel = $_POST['tel'];
  $fax = $_POST['fax'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $sd = $_POST['sd'];
  
  // query insert
  
  mysql_query("INSERT INTO orders (id,username,ip,date,method,cart,discount,subtotal,shipping,tax,total,shipping_zone,inv_name,inv_company,inv_addr1,inv_addr2,inv_state,inv_zip,inv_country,del_name,del_addr1,del_addr2,del_state,del_zip,del_country,tel,fax,email,message,sd)
  
  VALUES
  ('$id','$username','$ip','$order_date','$method','$cart','$discount','$subtotal','$shipping','$tax','$total','$shipping_zone','$inv_name','$inv_company','$inv_addr1','$inv_addr2','$inv_state','$inv_zip','$inv_country','$del_name','$del_addr1','$del_addr2','$del_state','$del_zip','$del_country','$tel','$fax','$email','$message','$sd')");
  
  ?&gt;
  </code></pre>
  
  <p>Link to mals-e I used the php_mals.zip file:</p>
  
  <p><a href="https://www.mals-e.com/tpv.php?tp=26" rel="nofollow">https://www.mals-e.com/tpv.php?tp=26</a></p>