PHP引用索引处的键值

时间:2011-05-30 01:20:36

标签: php mysql arrays smarty

嘿,我的表格中有一系列“数量”:

<tr>
                            <td class="col1"> Products </td>
                            <td class="col2">

                                <!-- Interests is an array of all interests in the database-->
                                {foreach $products as $product}
                                <select name="quantities[]" id ="quantities">
                                        {section name=quantities start=0 loop=$product.stock + 1 step=1}
                                        <option value="{$smarty.section.quantities.index}">{$smarty.section.quantities.index}</option>
                                        {/section}
                                </select>
                                {$product.name}<br>
                                {/foreach}
                            </td>
                        </tr>

将其传递给方法:

function add_order($customer, $delivery_address, $quantities)
{
    $connection = mysql_open();
    $customer = mysql_escape_string($customer);
    $delivery_address = mysql_escape_string($delivery_address);

    $query = "insert into SEOrders (name, address, status) " .
             "values ('$customer', '$delivery_address', 'New')";
    $result = mysql_query($query, $connection) or show_error();

    $id = mysql_insert_id();

    $products = get_products();

    for ($i = 0; $i < count($products); $i++)
    {
        if ($quantities[$i] > 0)
        {
            $product_id = $products[$i]['id'];
            $quantity = $quantities[$i];

            $query2 = "insert into SEOrder_items (order_id, product_id, quantity) " .
                    "values ($id, $product_id, $quantity)";
            $result2 = mysql_query($query2, $connection) or show_error();
        }
    }

    mysql_close($connection) or show_error();
    return $id;
}

然后我在这一行收到错误:

$query2 = "insert into SEOrder_items (order_id, product_id, quantity) " .
             "values ('$id', '$product_id', '$quantity')";
            $result2 = mysql_query($query, $connection) or show_error();

错误是:

Warning: mysql_query(): 4 is not a valid MySQL-Link resource in /net/SE/includes/defs.php on line 49 Error : 

有谁知道可能出错了什么?

1 个答案:

答案 0 :(得分:0)

你应该使用

$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');

不是

$connection = mysql_open();