PHP表单只将一个对象推送到数组

时间:2018-05-27 18:49:11

标签: php arrays object

每当类productEntry的新对象被推送到数组$ products时,最新的条目将覆盖最后一个条目。

    if (isset($_POST['formedit'])){
        $newproduct = new productEntry;
        $newproduct->productid = count($products);
        $newproduct->name = $_POST['productnamefield'];
        $newproduct->weight = $_POST['productweightfield'];
        $newproduct->length = $_POST['productlengthfield'];
        $newproduct->width = $_POST['productwidthfield'];
        $newproduct->height = $_POST['productheightfield'];
        $products[] = $newproduct;
    }

这是提交表单一次后的输出:

productEntry Object ( [productid] => 0 [name] => 1 [weight] => 1 [length] => 1 [width] => 1 [height] => 1 ) productEntry Object ( [productid] => 1 [name] => 2 [weight] => 2 [length] => 2 [width] => 2 [height] => 2 ) productEntry Object ( [productid] => 2 [name] => 3 [weight] => 3 [length] => 3 [width] => 3 [height] => 3 ) productEntry Object ( [productid] => 3 [name] => 4 [weight] => 4 [length] => 4 [width] => 4 [height] => 4 )

然后如果我再次使用不同的值提交表单

productEntry Object ( [productid] => 0 [name] => 1 [weight] => 1 [length] => 1 [width] => 1 [height] => 1 ) productEntry Object ( [productid] => 1 [name] => 2 [weight] => 2 [length] => 2 [width] => 2 [height] => 2 ) productEntry Object ( [productid] => 2 [name] => 3 [weight] => 3 [length] => 3 [width] => 3 [height] => 3 ) productEntry Object ( [productid] => 3 [name] => 5 [weight] => 5 [length] => 5 [width] => 5 [height] => 5 )

0 个答案:

没有答案