0

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.

I'm very new to PHP and have been trying to figure this out for days.

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.

For instance, when a customer has placed an order I would like for this to happen:

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.

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.

This is the code I use to display the quantity:

<?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();
?>

This is my form code:

<?php
echo "<form action=\"http://ww6.aitsafe.com/cf/add.cfm\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"userid\" value=\"12345678\">\n";
echo "<input type=\"hidden\" name=\"id\" value=\"6\">\n";
echo "<input type=\"hidden\" name=\"scode\" value=\"ABCD123\">\n";
echo "<input type=\"hidden\" name=\"nocart\">\n";
echo "<input type=\"hidden\" name=\"return\" value=\"http://www.mysite.co.uk\">\n";
echo "<b>Qty:</b> <input type=\"text\" name=\"qty\" size=\"1\">\n";
echo "<input type=\"hidden\" name=\"product\" value=\"Socks\">\n";
echo "<input type=\"hidden\" name=\"price\" value=\"1.85\">\n";
echo "<input name=\"submit\" value=\"Add to Cart\" type=\"submit\">\n";
echo "</form>";
?>

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:

<?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\');";

?>

I notice in this line of code:

echo "<form action=\"http://ww6.aitsafe.com/cf/add.cfm\" method=\"post\">\n";

Other people have a link to a php page:

echo "<form action=\"update.php\" method=\"post\">\n";

Does the link have to be to a php page?

I also have this:

<?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();
?>

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.

My htm code and php code are on separate pages, I echo the available quantity in the htm page.

Many Thanks.

This is the code that is used to update the database when an order is placed with remote call fro mals-e:

    <?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')");

?>

Link to mals-e I used the php_mals.zip file:

https://www.mals-e.com/tpv.php?tp=26

user2940698
  • 21
  • 2
  • 6

1 Answers1

0

I don't understand what you really want. here is a corrected version of the last piece of code you gave us (i removed your comments and put mines) :

<?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");   


$result = mysql_query("SELECT * FROM items
WHERE inventory='$inventory'") or die(mysql_error());  

while($row = mysql_fetch_array($result))


if        ($row !== false)
          {
           echo 'out of stock';
           }

else if  ($row['count']  == 0)
            {
            echo 'out of stock.';
            }

// only execute this request if there is stock
 else   
            {
           $sql = "UPDATE items SET inventory = (inventory -1) WHERE id = '6'";
            }

//what is this ?
//$result = mysql_query;

} 

BTW? do not use mysql_ functions, they are now deprecated. have a look at mysqli (http://fr.php.net/mysqli) or pdo (http://php.net/manual/fr/ref.pdo-mysql.php).

And to answer your question


I notice in this line of code:

echo "<form action=\"http://ww6.aitsafe.com/cf/add.cfm\" method=\"post\">\n";

Other people have a link to a php page:

echo "<form action=\"update.php\" method=\"post\">\n";

Does the link have to be to a php page?


Not really, this page can be anything. The thing is that if you want to process the form inputs with php, u'll have to call a page interpreted by php. By default apache recognizes php files those which have php extensions (*.php, *.php5 ...) but you can configure it to be able to handle all extensions you want (have a look at https://stackoverflow.com/a/12021997/2806497)

Community
  • 1
  • 1
OlivierH
  • 3,875
  • 1
  • 19
  • 32
  • OlivierH ~ thanks for that. I'm trying to get code to update the 'inventory' quantity in my database. When a customer has placed an order the quantity will be decreased by 1, then the new quantity available in 'inventory' will be updated and displayed on my product page. – user2940698 Nov 08 '13 at 10:28
  • Do you have the code executed when a customer places an order ? Can you add your own code to be executed at this moment ? How does this works ? What you got to do is add some code at the moment a customer places an order, with this code you update stock information in database for this product. There's also others solutions than php code if you can't add your own code : for example SQL Triggers. Read this : http://www.mysqltutorial.org/mysql-triggers.aspx. The thing is to intercept the moment an order is created in your database, and execute SQL at this moment, to update ordered product stock. – OlivierH Nov 08 '13 at 10:33
  • OlivierH ~ I've added the code used above that updates the database with order details, this is done with a remote call from mals-e. Explained on this page: https://www.mals-e.com/tpv.php?tp=26. I really don't understand and am trying to learn but I will take a look at the link you provided if it's easier for me to achieve what I want to do. Cheers! – user2940698 Nov 08 '13 at 11:00