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: