自动更新数据库所需的代码 - 需要修复

时间:2013-11-08 10:03:02

标签: php mysql

我真的可以提供一些帮助。我已成功设法使用PHP代码在我的产品页面上显示我有多少商品,并得到其他成员的帮助,我非常感激。

我是PHP的新手,并且已经尝试了好几天。

我有一个数据库设置了一个名为'items'的表,如果可能的话我想自动更新。我知道我可以手动完成这项工作,但我想防止过度销售我没有的库存。

例如,当客户下订单时,我希望这样做:

1)获取“库存”下的库存商品数量,并将数量减少1。 2)让库存中的新数量替换产品页面上显示的旧数量。

现在,我将Mals-e用于购物车,并通过远程调用将所有订单发送到数据库。这样工作正常,因此信息无法进入数据库。

这是我用来显示数量的代码:

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

这是我的表单代码:

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

这些是我认为可能需要的其他代码,但我不知道如何使用它们或者它们是否真的需要我们:

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

?>

我注意到这行代码:

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

其他人有一个指向php页面的链接:

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

链接必须是php页面吗?

我也有:

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

我知道这不对,我完全错了。有人可以用我需要的代码示例指出正确的方向,因为我认为我需要更多的代码才能让它发挥作用。

我的htm代码和php代码在不同的页面上,我回显了htm页面中的可用数量。

非常感谢。

这是用于通过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')");

?>

链接到mals-e我使用了php_mals.zip文件:

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

1 个答案:

答案 0 :(得分:0)

我不明白你真正想要的是什么。这是您给我们的最后一段代码的更正版本(我删除了您的评论并放置了地雷):

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

} 

顺便说一句?不要使用mysql_函数,现在不推荐使用它们。看看mysqli(http://fr.php.net/mysqli)或pdo(http://php.net/manual/fr/ref.pdo-mysql.php)。

并回答你的问题


我注意到这行代码:

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

其他人有一个指向php页面的链接:

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

链接必须是php页面吗?


不是这样,这个页面可以是任何东西。问题是,如果你想用php处理表单输入,你将不得不调用由php解释的页面。默认情况下,apache识别那些具有php扩展名(* .php,* .php5 ...)的php文件,但你可以将其配置为能够处理你想要的所有扩展(看看https://stackoverflow.com/a/12021997/2806497