【问题标题】:Array field saving same value for all fields数组字段为所有字段保存相同的值
【发布时间】:2012-09-22 22:57:52
【问题描述】:

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

例如:

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;

});

【问题讨论】:

    标签: php arrays duplicates field


    【解决方案1】:

    那是因为您使用的是$_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-02-28
      • 2013-10-23
      • 2020-10-18
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多