PHP ссылается на значение ключа в индексе - PullRequest
0 голосов
/ 30 мая 2011

Эй, у меня есть массив "количеств" из моей формы:

<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 голосов
/ 30 мая 2011

вы должны использовать

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

не

$connection = mysql_open();

...