0

当我将图像和“有序”字段保存到数据库时,它会保存图像,但每个“有序”字段都保存相同的值。关于如何修复它以便“有序”字段正确保存我为每个字段输入的值的任何想法?

例如:

image_id | 产品编号 | 订购 | 图片 | 缩略图
-------------------------------------------------- ------------------
第593章 94 | 35 | a23145b.jpg | a231458.jpg
-------------------------------------------------- ------------------
第592章 94 | 35 | 1348352.jpg | 1348358.jpg

这是我目前所拥有的:

    if (isset($_FILES['product_image'])) {

        $files = array();

        foreach ($_FILES['product_image'] as $key => $values) {

            foreach ($values as $i => $value)
                $files[$i][$key] = $value;

        }

        foreach ($files as $file) {

            if (!empty($file['name'])) {

                $image = $upload->upload_image($file);
                $thumb = $upload->upload_image($file);

                $values = array(
                    'product_id'    => $product_id, 
                    'image'         => $image,
                    'thumbnail'     => $thumb,
                    'ordered' =>  $_POST['ordered'][0]
                );  

                $db->insert(config_item('cart', 'table_product_images'), $values);

            }

        }

    }

和字段的js

var image_row = 0;

$('#add_image').click(function() {

    html  = '<tr id="image' + image_row + '">';
    html += '<td><input type="file" name="product_image[' + image_row + ']" /></td>';
    html += '<td><input type="text" name="ordered[' + image_row + ']" /></td>';
    html += '<td><a href="#" id="remove_' + image_row + '" class="remove_image button orange">Remove</a></td>';
    html += '</tr>';

    $('#images table > tbody').append(html);

    image_row++;

    return false;

});
4

1 回答 1

0

那是因为你正在使用$_POST['ordered'][0]. 索引应该是动态的。将您的代码更改为:

foreach ($files as $key => $file) {

        if (!empty($file['name'])) {

            $image = $upload->upload_image($file);
            $thumb = $upload->upload_image($file);

            $values = array(
                'product_id'    => $product_id, 
                'image'         => $image,
                'thumbnail'     => $thumb,
                'ordered' =>  $_POST['ordered'][$key]
            );  

            $db->insert(config_item('cart', 'table_product_images'), $values);

        }

    }
于 2012-09-22T22:59:52.020 回答