PHP MySQL插入数据库时出错

PHP MySQL插入数据库时出错,php,mysql,sql,arrays,insert,Php,Mysql,Sql,Arrays,Insert,你好。当我使用以下功能时: 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

你好。当我使用以下功能时:

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
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;
但只是偶尔。我不知道为什么它只是偶尔发生

此外,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;
}
其中主机、用户。。。etc在单独的文件中定义

传入的$id直接来自页面,我已经检查过了。$order_项目通过以下方式传入:

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;
}
美元产品通过以下方式传递:

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;
}
如果相关,数据库如下所示:

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
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;
基本上,我想做的是从当前库存中减去数量,同时将数量添加到已售出的库存中。然而,这不是很有效,我会时不时地得到上面描述的错误。但奇怪的是,我会多次调用该函数,没有问题

提前感谢,,
JheeBz

您需要从函数get\u order\u items内的数据库中选择字段product\u id。p、 product_id.

只需替换此功能,这可能会起作用

function get_order_items($id)
{
    $connection = mysql_open();
    $query = "select p.id, oi.order_id, p.name, oi.quantity, p.stock, oi.product_id " .
             "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;
}

那么在这种情况下,我需要加入吗?我在连接方面有点差劲。已经有一个连接:将SEProducts作为p连接。只需在SELECT和FROM之间添加p.product_id。