嘿。當我使用下面的函數:插入數據庫時PHP MySQL錯誤
function ship_order($id, $order_items, $products)
{
$connection = mysql_open();
foreach ($order_items as $order_item)
{
foreach ($products as $product)
{
if ($order_item['product_id'] == $product['id'])
{
if ($order_item['quantity'] > $product['stock'])
{
return false;
}
}
}
}
$query = "update SEOrders " .
"set status='Shipped' " .
"where id = $id";
$result = mysql_query($query, $connection) or show_error();
foreach ($order_items as $order_item)
{
foreach ($products as $product)
{
if ($order_item['product_id'] == $product['id'])
{
$new_stock = $product['stock'] - $order_item['quantity'];
$sold_stock = $product['sold_stock'] + $order_item['quantity'];
$query = "update SEProducts " .
"set sold_stock= '$sold_stock', stock='$new_stock' " .
"where id = $id";
$result = mysql_query($query, $connection) or show_error();
}
}
}
mysql_close($connection) or show_error();
return true;
}
我偶爾會得到它看起來像這樣的錯誤:
Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 176 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/home/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Notice: Undefined index: product_id in /net/export/home/public_html/wp/SE/includes/defs.php on line 196 Warning: Cannot modify header information - headers already sent by (output started at /net/export/home/public_html/wp/SE/includes/defs.php:196) in /net/export/home/public_html/wp/SE/ship_order_action.php on line 21
但只飄飛。我不知道爲什麼它只發生在有時。
此外,mysql_open()函數的定義如下:
function mysql_open()
{
$connection = @ mysql_connect(HOST, USER, PASSWORD)
or die("Could not connect");
mysql_select_db(DATABASE, $connection)
or show_error();
return $connection;
}
凡主機,用戶...等,在一個單獨的文件中定義。
傳入的$ id是直接從頁面中進行檢查的。
function get_order_items($id)
{
$connection = mysql_open();
$query = "select oi.order_id, p.name, oi.quantity, p.stock " .
"from SEOrder_items as oi join SEProducts p on oi.product_id = p.id " .
"where oi.order_id = $id";
$result = mysql_query($query, $connection) or show_error();
mysql_close($connection) or show_error();
$order_items = array();
while ($order_item = mysql_fetch_array($result))
{
$order_items[] = $order_item;
}
return $order_items;
}
和$產品通過傳遞:
function get_products()
{
$connection = mysql_open();
$query = "SELECT * FROM SEProducts";
$query .= " order by name asc";
$result = mysql_query($query, $connection) or show_error();
$products = array();
while ($product = mysql_fetch_array($result))
{
$products[] = $product;
}
mysql_close($connection) or show_error();
return $products;
}
在情況下,它相關的數據庫看起來是這樣的:
create table if not exists SEOrders
(
id int not null auto_increment primary key,
name varchar(20) not null,
address varchar(30) not null,
status varchar(10) not null,
dateShipped timestamp
)ENGINE=INNODB;
create table if not exists SEProducts
(
id int not null auto_increment primary key,
price double not null,
name varchar(30) not null,
stock int not null,
original_stock int not null,
sold_stock int not null
)ENGINE=INNODB;
create table if not exists SEOrder_items
(
id int not null auto_increment primary key,
order_id int not null,
foreign key (order_id) references SEOrders(id),
product_id int not null,
foreign key (product_id) references SEProducts(id),
quantity int not null
)ENGINE=INNODB;
基本上$ ORDER_ITEMS通過傳遞我試圖做的是從當前股票中減去數量,並將數量加入到賣出的股票中。然而,這不是很有效,每隔一段時間我會得到上述錯誤。但奇怪的是,我會多次調用該函數而沒有任何問題。
由於提前, JheeBz
那麼在那種情況下,我必須做一個連接?我有點吮吸連接。 – JheeBz 2011-05-30 11:43:32
已經有一個連接:'將SEProducts作爲p'加入。只需在SELECT和FROM之間添加'p.product_id'。 – 2011-05-30 11:45:51
謝謝,但可悲的是,這仍然觸發了錯誤。 – JheeBz 2011-05-30 12:06:59